scurtis_1
asked on
XSL: How to ignore empty tags?
Hi,
The following XSL looks for all occurences of the JobPackURL tag and produces a TemporaryItemDS tag for each one. How do I check to see if the JobPackURL tag has any content and if it is empty, don't produce the TemporyItemDS tag?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TemporaryItemDs[las t()]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:for-each select="../FieldSupportInf ormation/J obPack/Job PackURL">
<TemporaryItemDs>
<TemporaryItemCode>JBPCK<x sl:value-o f select="position()"/></Tem poraryItem Code>
<TemporaryItemValue>"<xsl: value-of select="." />"</TemporaryItemValue>
</TemporaryItemDs>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Thanks
Scott
The following XSL looks for all occurences of the JobPackURL tag and produces a TemporaryItemDS tag for each one. How do I check to see if the JobPackURL tag has any content and if it is empty, don't produce the TemporyItemDS tag?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TemporaryItemDs[las
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:for-each select="../FieldSupportInf
<TemporaryItemDs>
<TemporaryItemCode>JBPCK<x
<TemporaryItemValue>"<xsl:
</TemporaryItemDs>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Thanks
Scott
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Must one use a conditional on *every single* node that might possibly be empty? Is there a way to declare from the beginning of the stylesheet that you don't want to output empty tags? It seems verbose and inefficient to have to use an <xsl:if> for every tag... it just seems to add unneccesary length. Any ideas on how to do this more efficiently/elegantly? I have a pretty long list of nodes to process and I dread having to write a conditional for every one. Hopefully, there's a better way?