user3376375
user3376375

Reputation: 7

How to create an attribute?

my 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>

my 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>   

I want to create internal links in the output html document. My templates are created in every "section/title" attribute id, but I do not want to get the id attribute with the value "null". In output I want get <H2 id="chapter-introduction">Introduction</H2> ... <H2>Problem description</H2>

Upvotes: 0

Views: 50

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117165

How about something simple:

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

Upvotes: 3

helderdarocha
helderdarocha

Reputation: 23627

You can surround the <xsl:attribute> with a test checking for the presence of the id attribute, so it will only be processed if the <title> actually contains it:

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

Upvotes: 1

Related Questions