2014-03-27 76 views
0

我的XML如何创建属性?

<article> 
<section> 
    <title id="chapter-introduction">Introduction</title> 

    <para>Some text</para> 
</section> 

<section> 
    <title>Problem description</title> 

    <para>Some text</para> 
    <para>Please click <link linkend="chapter-introduction">here</link> to 
    go to the Introduction chapter.</para> 
</section> 
</article> 

我XSL

<xsl:template match="section/title"> 
<H2> 
    <xsl:attribute name="id"> 
    <xsl:value-of select="@id" /> 
    </xsl:attribute> 
    <xsl:value-of select="."/> 
</H2> 
</xsl:template> 


<xsl:template match="link"> 
<u>  
    <a style="color:green" href="#{@linkend}"> 
    <xsl:value-of select="."/> 
    </a> 
</u> 
</xsl:template> 

我想创建输出HTML文档中的内部链接。我的模板是在每个“section/title”属性id中创建的,但我不想获得值为“null”的id属性。 在输出我想要得到<H2 id="chapter-introduction">Introduction</H2> ... <H2>Problem description</H2>

回答

1

您可以环绕<xsl:attribute>与该id属性的存在的测试检查,所以才会被处理。如果<title>实际上包含了它:

<xsl:if test="@id"> 
    <xsl:attribute name="id"> 
     <xsl:value-of select="@id" /> 
    </xsl:attribute> 
</xsl:if> 
3

怎么样简单:

<xsl:template match="section/title"> 
    <H2> 
     <xsl:copy-of select="@id" /> 
     <xsl:value-of select="."/> 
    </H2> 
</xsl:template>