webForumDet fria alternativet

xml /xsl hjälp

2 svar · 417 visningar · startad av clown

clownMedlem sedan juni 2000126 inlägg
#1

HEj,

jag sitter och försöker göra en stylesheet som bara skall plocka ut en del av ett xml dokument, men jag får inte stylesheetet att fungera?!?

Tanken är att jag skall ha en hel bok i xml format med kapitel och underkapitel. Det jag vill är att med hjälåp av chapter.xsl enbart få ut ett kapitel. Men när jag titta på xml filen kopplad till denna xsl fil så kommer hela xml informationen med...

xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="C:\Inetpub\wwwroot\xml\chapter.xsl"?>
<book>
	<book_name>Authers Manual</book_name>
	<chapter id="1">
		<chapter_header>Chapter 1</chapter_header>
		<subchapter id="1">
			<subchapter_header>Sub chapter 1</subchapter_header>
			<para header="Paragraph header 1 in sub 1" id="1">In here we would like to <b>have</b> som text about sub chapter 1</para>
		</subchapter>
		<subchapter id="2">
			<subchapter_header>Sub chapter 2</subchapter_header>
			<para header="Paragraph header 1 in sub 2" id="1">In here <i>we</i> would like to <b>have</b> som text </para>
			<para header="Paragraph header 2 in sub 2" id="2">In here we would like to <b>have</b> som text </para>
			<para header="Paragraph header 3 in sub 2" id="3">In here we would like to <b>have</b> som text </para>
		</subchapter>
	</chapter>
	<chapter id="2">
		<chapter_header>Chapter 2</chapter_header>
		<subchapter id="1">
			<subchapter_header>Sub chapter 1</subchapter_header>
			<para header="Paragraph header 1 in sub 1" id="1">In here we would like to <b>have</b> som text about sub chapter 1</para>
		</subchapter>
		<subchapter id="2">
			<subchapter_header>Sub chapter 2</subchapter_header>
			<para header="Paragraph header 1 in sub 2" id="1">In here we would like to <b>have</b> som text </para>
			<para header="Paragraph header 2 in sub 2" id="2">In here we would like to <b>have</b> som text </para>
			<para header="Paragraph header 3 in sub 2" id="3">In here we would like to <b>have</b> som text </para>
		</subchapter>
	</chapter>
</book>

chapter.xsl :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/book/chapter[@id=1]/*">
    <html>
      		<head>
			<title></title>
		</head>
	<body>
	---title:<xsl:value-of select="chapter_header"/>--- 
      <xsl:apply-templates/>
	  </body>
    </html>
  </xsl:template>

  <xsl:template match="/book/chapter[@id=1]">
	    <p align="center"><font face="Arial" size="4"><xsl:value-of select="chapter_header"/></font></p>
	      <xsl:apply-templates select="subchapter"/>   
</xsl:template>
  
  <xsl:template match="/book/chapter[@id=1]/subchapter">
	    	<p align="center">
    		<i><font face="Arial" size="3"><xsl:value-of select="subchapter_header"/></font></i><br></br><br></br>
    		<xsl:apply-templates select="para"/>
		</p>
  </xsl:template>

  <xsl:template match="/book/chapter[@id=1]/para">
	    	<p align="center">
    		<xsl:apply-templates select="@header"/>
		<font face="Arial" size="2"><xsl:value-of select="."/></font>
		</p>
  </xsl:template>

    <xsl:template match="/book/chapter[@id=1]/para/@header">
	<font face="Arial" size="2"><b><xsl:value-of select="."/></b></font><br></br>
    </xsl:template>

  
</xsl:stylesheet>

Hjälp!!!
Hur får jag bara ut ett kapitel??

/Jonas

jarvkloMedlem sedan juli 20013 378 inlägg
#2

Här är ett sätt:

Notera:
* Utplockningen av chapter_header.
* select-satsen i det första <apply-templates>
* kvalificeringen på elementnamn utan fullständig path (eftersom du har <apply-templates select="..." /> i varje delregel behövs inte /book/chapter/... där

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/book">
    <html>
      		<head>
			<title></title>
		</head>
	<body>
	---title:<xsl:value-of select="chapter[@id=1]/chapter_header"/>--- 
      <xsl:apply-templates select="chapter[@id=1]" />
	  </body>
    </html>
  </xsl:template>

  <xsl:template match="chapter">
	    <p align="center"><font face="Arial" size="4"><xsl:value-of select="chapter_header"/></font></p>
	      <xsl:apply-templates select="subchapter"/>   
</xsl:template>
  
  <xsl:template match="subchapter">
	    	<p align="center">
    		<i><font face="Arial" size="3"><xsl:value-of select="subchapter_header"/></font></i><br></br><br></br>
    		<xsl:apply-templates select="para"/>
		</p>
  </xsl:template>

  <xsl:template match="para">
	    	<p align="center">
    		<xsl:apply-templates select="@header"/>
		<font face="Arial" size="2"><xsl:value-of select="."/></font>
		</p>
  </xsl:template>

    <xsl:template match="@header">
	<font face="Arial" size="2"><b><xsl:value-of select="."/></b></font><br></br>
    </xsl:template>

  
</xsl:stylesheet>
clownMedlem sedan juni 2000126 inlägg
#3

AAhhh!!!
Lysande!!!

Så nära jag var ;-)!!!

Tack än en gång!

/Jonas

135 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)