1

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.

3
  • Can you show the XSLT you currently have? (Maybe at least the XSLT that currently transforms the text into ` div`) Thanks! Commented Jun 8, 2016 at 21:55
  • Tim, here is the one that I will use : <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="entry"> <div> <xsl:value-of select="text"/> </div> </xsl:template> </xsl:stylesheet> Commented Jun 8, 2016 at 22:17
  • 1
    Can you edit your question to show this XSLT, as code is always hard to read in comments? Thanks! Commented Jun 8, 2016 at 22:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant Tim. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.