By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

<need help>How to print the preceding elem value based on descendant node in XSLT?

P: 17
Hi,
In the folowing XML snippet:

Expand|Select|Wrap|Line Numbers
  1. <template1>
  2. <name>student</name>
  3. <elem1>
  4. <subelem1>65</subelem1>
  5. <subelem2>15</subelem2>
  6. </elem1>
  7. <elem2>100</elem2>
  8. </template1>
  9.  
  10. </root>
  11.  
For the above code,I need to display an error:
"elem2 node is not allowed for name=student as subelem1 is greater than 50"

so the requirement is,elem2 should not be present if subelem1 is > 50.

I have the following XSLT code:

<xsl:template match="/root/template1/elem1/subelem1">
<xsl:variable name="value" select="." />
<xsl:if test="$value > 50" >
subelem1 is greater than 50
<xsl:if test="../../elem2" >
<xsl:message terminate="yes">
<xsl:value-of select="concat("elem2 is not allowed for name=....")/>
elem4 exists
</xsl:if>
</xsl:if>
</xsl:template>

Using the above code,I am able to print the error as "elem2 is not allowed for name= ".
Here I am not able to print "name=student".How to print the value of name node?

Also please note that there will be 10 <template1> nodes.so the XSLT has to work for each template1.

Please reply me how to achieve this.

Thanks,
Sep 16 '08 #1
Share this Question
Share on Google+
5 Replies


jkmyoung
Expert 100+
P: 2,057
1. Since you need it to run for every template1 node, I suggest you change the template to match template1.
2. Use paths relative to template1 node. Note change in variable value.

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="template1">
  2.   <xsl:variable name="value" select="elem1/subelem1"/>
  3.   <xsl:if test="$value &gt; 50">
  4. subelem1 is greater than 50
  5.     <xsl:if test="elem2">
  6.       <xsl:message terminate="yes"/>
  7.         <xsl:value-of select="concat('elem2 is not allowed for name=', name)"/>
  8.       elem4 exists
  9.     </xsl:if>
  10.   </xsl:if>
  11. <xsl:template>
  12.  
Sep 16 '08 #2

P: 17
Hi young,
Thanks for your reply but I am sorry that it didnt work.what else can be changed to this?

Thanks,

1. Since you need it to run for every template1 node, I suggest you change the template to match template1.
2. Use paths relative to template1 node. Note change in variable value.

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="template1">
  2.   <xsl:variable name="value" select="elem1/subelem1"/>
  3.   <xsl:if test="$value &gt; 50">
  4. subelem1 is greater than 50
  5.     <xsl:if test="elem2">
  6.       <xsl:message terminate="yes"/>
  7.         <xsl:value-of select="concat('elem2 is not allowed for name=', name)"/>
  8.       elem4 exists
  9.     </xsl:if>
  10.   </xsl:if>
  11. <xsl:template>
  12.  
Sep 16 '08 #3

jkmyoung
Expert 100+
P: 2,057
If you need to use the same template (subelem1) you can use <xsl:for-each ..\.. to change context to the template1 node:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/root/template1/elem1/subelem1">
  2.   <xsl:for-each select="..\..">
  3.     all previous code
  4.  
Sep 16 '08 #4

P: 17
Hi young,
Its working now.Thanks a lot.


If you need to use the same template (subelem1) you can use <xsl:for-each ..\.. to change context to the template1 node:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/root/template1/elem1/subelem1">
  2.   <xsl:for-each select="..\..">
  3.     all previous code
  4.  
Sep 16 '08 #5

Dormilich
Expert Mod 5K+
P: 8,639
I would have gone for:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="//template1">
  2.   <xsl:if test=".[elem2][elem1/subelem1 &amp;gt; 50]">
  3. ... error message here ...
  4.   </xsl:if>
  5. </xsl:template>
regards
Sep 16 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.