Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xsl - differentiating nodes

 
Brendan Kennedy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Does anybody know how to diffentiate between nodes that only have different attribute names. My code is as follows:
<xsl:when test="REVENUE/@NAME = 'ACTUAL'">
<xsl:choose>
<xsl:when test="not(string(REVENUE/PAX_NUM))">-</xsl:when>
<xsl therwise><xsl:value-of select="REVENUE/PAX_NUM"/>
</xsl therwise>
</xsl:choose>
</xsl:when>
<xsl:when test="REVENUE/@NAME = 'BUDGET'">
<xsl:choose>
<xsl:when test="not(string(REVENUE/PAX_NUM))">-</xsl:when>
<xsl therwise><xsl:value-of select="REVENUE/PAX_NUM"/>
</xsl therwise>
</xsl:choose>
</xsl:when>
This just presents the information in ACTUAL twice... The xml is as follows: -
<REVENUE NAME="ACTUAL">
<PAX_NUM>10<PAX_NUM>
</REVENUE>
<REVENUE NAME="BUDGET">
<PAX_NUM>20<PAX_NUM>
</REVENUE>
Thanks,
Brendan
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I put repeated part of your test in a separate template and named it "PAX_NUM" (whatever it means ) - to prevent code duplication (you would do it anyway at later stages of development ) Next point is more serious: I do not know how exactly you call your two tests, but it looks like you do not follow XML document structure, but first choose all REVENUE elements with NAME value of ACTUAL, then all REVENUE with NAME of 'BUDGET'. From your question "how to diffentiate between nodes" I assume that you want to process XML input how it is. In this case you can have two <xsl:template> element which will match your two cases:
<xsl:template match="REVENUE[./@NAME = 'ACTUAL']">
and
<xsl:template match="REVENUE[./@NAME = 'BUDGET']">
The whole stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="REVENUE[./@NAME = 'ACTUAL']">
<xsl:call-template name="PAX_NUM"/>
</xsl:template>
<xsl:template match="REVENUE[./@NAME = 'BUDGET']">
<xsl:call-template name="PAX_NUM"/>
</xsl:template>
<xsl:template name="PAX_NUM">
<xsl:choose>
<xsl:when test="not(string(./PAX_NUM))">-</xsl:when>
<xsl:otherwise><xsl:value-of select="./PAX_NUM"/>
</xsl: otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The output is:
10
20
Of course, in your case both tests/templates do the same, so there is no real need to differentiate But you can have different elements inside your templates.

[This message has been edited by Mapraputa Is (edited July 31, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic