Reputation: 19
I'm rather new to XML and apologies if it's not up to scratch.
The following is my code, which just doesn't seem to work. And I think I know why. I am trying to have it set so that if there is a shipID then the shipping address should show, on the table/div, but if there is a contact number instead this should show. I think it may be something to do with the 'shipID' as it is stated as 'less than' but I have since made the ShipID text and NOT a number. Is there a way to basically say "If there is a shipID then show this" instead of it having to be 'if there is a ship number less than"?
Cheers.
<tr style= "color:white; background:yellow;">
<th> header 1</th>
<th> header 2</th>
<th> header 3</th>
</tr>
<xsl:for-each select="shipping">
<xsl:choose>
<xsl:when test="shipID < '2'">
<tr style="color:black;">
<td>
<xsl:value-of select="header 1"/>
</td>
<td>
<xsl:value-of select="header 2"/>
</td>
<td>
<xsl:value-of select="header 3"/>
</td>
</tr>
</xsl:when>
<tr style= "color:white; background:yellow;">
<th> contact 1</th>
<th> contact 2</th>
<th> contact 3</th>
</tr> <xsl:otherwise>
<xsl:for-each select="contacts">
<tr style="color:black;">
<td>
<xsl:value-of select="contact1"/>
</td>
<td>
<xsl:value-of select="Contact2"/>
</td>
<td>
<xsl:value-of select="Contact3"/>
</td>
</tr>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
Upvotes: 0
Views: 79
Reputation: 477
XSLT's default behaviour is to navigate the XML by itself and for that reason it is rarely necessary to explicitly code loops with for-each.
Without seeing the rest of your XML and code it is not possible to give a complete solution and I would not want to anyway because I am trying to encourage you to think about XSLT in a different way.
If you want something to happen for every shipID and something else to happen for every contact then absent of further information the most straightforward thing to do is to write a template rule that will fire when those nodes are encountered in the input tree. Like so
<xsl:template match="shipID">
..do the shipID stuff
</xsl:template>
<xsl:template match="contacts">
..do the contacts stuff
</xsl:template>
Those rules will fire (if necessary) if you either apply-templates to their parent or allow the default XSLT processing to process their parent.
XSLT is alot easier to program if you take out time to understand what the built-in default rules do because you don't end up re-implementing what would have been done for you anyway. In the course of learning those you will probably have to learn what apply-templates does but learning these 2 concepts will make most of what you need to do with XSLT relatively trivial.
Upvotes: 1
Reputation: 116993
Is there a way to basically say "If there is a shipID then show this"
Sure there is:
<xsl:when test="shipID">
<!-- some code -->
</xsl:when>
will apply some code
when an element shipID
exists as a child of the current node (shipping
in your example). Note that "exists" means just that, not that it necessarily has a value.
Upvotes: 1