File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes xsl - differentiating nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xsl - differentiating nodes" Watch "xsl - differentiating nodes" New topic
Author

xsl - differentiating nodes

Brendan Kennedy
Ranch Hand

Joined: May 02, 2001
Posts: 65
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

Joined: Aug 26, 2000
Posts: 10065
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).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xsl - differentiating nodes