Reputation: 3707
I have an XML file formatted like
<paragraph> Some Free text goes here<LinkType1 href="link1" >LinkName1</LinkType1> Then some more text <LinkType2 href="link2" >LinkName2</LinkType2>Then some more text <LinkType1 href =link3" >LinkName3</LinkType1> and then some more text
</paragraph>
This XML represents a paragraph of text with some links embedded inside it. In other words it is text and nodes within this text.
I need to convert it to HTML that looks like:
<p>
Some Free text goes here<a href="link1" target="_blank" >LinkName1</a> Then some more text <a href="link2" target="_blank" >LinkName2</a>Then some more text <a href =link3" target="_blank" >LinkName3</a> and then some more text
</p>
How can I do such transformation with XSLT ?
Upvotes: 1
Views: 175
Reputation: 243599
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="paragraph">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'LinkType')]">
<a href="{@href}" target="_blank" >
<xsl:value-of select="."/>
</a>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (corrected to become well-formed !):
<paragraph> Some Free text goes here
<LinkType1 href="link1" >LinkName1</LinkType1> Then some more text
<LinkType2 href="link2" >LinkName2</LinkType2>Then some more text
<LinkType1 href ="link3" >LinkName3</LinkType1> and then some more text
</paragraph>
produces the wanted, correct result:
<p> Some Free text goes here
<a href="link1" target="_blank">LinkName1</a> Then some more text
<a href="link2" target="_blank">LinkName2</a>Then some more text
<a href="link3" target="_blank">LinkName3</a> and then some more text
</p>
Upvotes: 2