2011-04-16 84 views
2

所以我有一个包含XSLT包裹节点文本

... 
<chapter> 
    <para>This line has a quote <quote id="one"/>. Here is some more text.</para> 
    <para>This also has a quote <quote id="two"/>. Here is some more text.</para> 
</chapter> 
<references> 
    <source id="one"> 
     <author>Author 1</author> 
     <title>Title 1</title> 
     <year>2001</year> 
    </source> 
    <source id="two"> 
     <author>Author 2</author> 
     <title>Title 2</title> 
     <year>2002</year> 
    </source> 
</references> 
... 

一个XML文件,我想输出一个XHTML

... 
<p>This line has a quote <a href="#one>[1]</a>. Here is some more text.</p> 
<p>This also has a quote <a href="#two>[2]</a>. Here is some more text.</p> 
<h3>References</h3> 
<ol> 
    <li><a name="one">Author 1, Title 1, 2001</a></li> 
    <li><a name="two">Author 2, Title 2, 2002</a></li> 
</ol> 
... 

所以,我要的是里面的文字报价与链接到参考列表中的项目。 我也想要参考文本中出现的顺序。

回答

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

    <xsl:template match="para"> 
     <p><xsl:apply-templates/></p> 
    </xsl:template> 

    <xsl:template match="quote"> 
     <a href="#{@id}"> 
     <xsl:text>[</xsl:text> 
     <xsl:number count="quote" level="any" /> 
     <xsl:text>]</xsl:text> 
     </a> 
    </xsl:template> 

    <xsl:template match="references"> 
     <h3>References</h3> 
     <ol> 
      <xsl:apply-templates/> 
     </ol> 
    </xsl:template> 

    <xsl:template match="source"> 
     <li> 
      <a name="{@id}"> 
       <xsl:apply-templates/> 
      </a> 
     </li> 
    </xsl:template> 

    <xsl:template match="author|title"> 
     <xsl:value-of select="."/> 
     <xsl:text>, </xsl:text> 
    </xsl:template> 

    <xsl:template match="year"> 
     <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

+1正确答案。我会用',< XSL:模板>'。 – 2011-04-17 21:37:41

+0

对于正确的答案+1。 – Flack 2011-04-18 05:20:40