• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

xsl - differentiating nodes

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic