Reputation: 137
I need to write a clause into my xslt that says if an element is present display text node and if it is not present, display nothing. I can find how to write if the text node is a particular word but not if the element is present.
Any advice is much appreciated.
PS: new to xslt/xml etc
Eg: XML represents a book containing pages. One version of the page has a header. Underneath is a table with four columns and 20 rows. Under this is a footer. This footer is not on the other version of the page. My xslt needs to transform the xml into a webpage representing this visually.
the xml therefore has an element of <Footer>
which has a minOccurs of 0 in the schema.
Upvotes: 3
Views: 892
Reputation: 14305
This can be done by omitting the comparison e.g.
<xsl:if test='root/element'>
However, a cleaner way ultimately getting what you want is to use xsl:templates
So for xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<page>
<title>Franks</title>
<header>header text</header>
<bodytext>here is the body text</bodytext>
</page>
<page>
<title>Joes</title>
<footer>footer text</footer>
<bodytext>here is the body text2</bodytext>
</page>
</root>
The xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="page"></xsl:apply-templates>
</xsl:template>
<xsl:template match="page">
<h1>
<xsl:value-of select="title"/>
</h1>
<p>
<xsl:value-of select="bodytext"/>
</p>
<xsl:apply-templates select="footer"/>
</xsl:template>
<xsl:template match="footer">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
illustrates the way this can be done. check out w3schools xpath tutorial for more information on selecting.
Upvotes: 4