Following is sample XML File –
<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
<Book>
<AName>Steven Holzner</AName>
<BName>Using XSLT</BName>
<Pub>ABC Publication </Pub>
<Web>http://www.ABCPub.com</Web>
</Book>
<Book>
<AName>Steven Holzner</AName>
<BName>Using HTML</BName>
<Pub>XYZ Publication </Pub>
<Web></Web>
</Book>
</Catalog>
I want to hyperlink the Pub name if Web address is given, other wise only Pub name w/o a link…
My XSL code is here –
<xsl:template match="Catalog">
<xsl:for-each select="Book">
<p><a>
<xsl:attribute name="href">
<xsl:value-of select="Web"/>
</xsl:attribute>
<xsl:value-of select="Pub"/>
</a></p>
</xsl:for-each>
</xsl:template>
I want following HTML source –
<p><a href=”http://www.ABCPub.com”>ABC Publication</a></p>
<p>XYZ Publication</a></p>
Rightnow, this XSL gives hyperlink tag even if Web addr is not given..
What piece of code will do the needful..?
have a nice day –
John
This can be achieved with a very short and simple transformation — no
<xsl:for-each>, no<xsl:if>,<xsl:choose>,<xsl:when>,no<xsl:attribute>at all:when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation: Template pattern matching.