sumanmodi
sumanmodi

Reputation: 13

Need to transform an xml using XSLT

I am working on an XSLT where I transforming an XML into an nice looking html markup.

I have below input XML :

<entry in_force_from="20011201" colname="2" morerows="0">
    <text in_force_from="20011201" newpara="N">A
        <autodeftext glossary-id="G430"> firm</autodeftext> must conduct its business with integrity.
    </text>
</entry>

And I want to transform this into :

<div>
    A<a href="hell.aspx?id=G430"> firm</a> must conduct its business with integrity.
</div>

Most of the transformation is quite straight forward except the creation of this a link node.

Upvotes: 1

Views: 34

Answers (1)

Tim C
Tim C

Reputation: 70598

Instead of using xsl:value-of to get the value of the text node, you should use xsl:apply-templates to allow you to add more templates to transform the descendant nodes

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/> 
  <xsl:template match="entry"> 
    <div> 
      <xsl:apply-templates select="text"/> 
    </div> 
  </xsl:template> 

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

  <xsl:template match="autodeftext">
    <a href="hell.aspx?id={@glossary-id}">
       <xsl:apply-templates />
    </a>
  </xsl:template> 
</xsl:stylesheet>

Strictly speaking, you could actually remove the template matching text here as XSLT's built-in template rules will do the same thing in this case anyway.

Note the use of Attribute Value Templates in creating the href attribute, to simplify the code.

Upvotes: 1

Related Questions