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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • njsimha
    New Member
    • Sep 2008
    • 17

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

    Hi,
    In the folowing XML snippet:

    Code:
    <template1>
    <name>student</name>
    <elem1>
    <[B]subelem1[/B]>65</[B]subelem1[/B]>
    <subelem2>15</subelem2>
    </elem1>
    <[B]elem2[/B]>100</[B]elem2[/B]>
    </template1>
    
    </root>
    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,
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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.

    [code=xml]
    <xsl:template match="template 1">
    <xsl:variable name="value" select="elem1/subelem1"/>
    <xsl:if test="$value &gt; 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=', name)"/>
    elem4 exists
    </xsl:if>
    </xsl:if>
    <xsl:template >
    [/code]

    Comment

    • njsimha
      New Member
      • Sep 2008
      • 17

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

      Thanks,

      Originally posted by jkmyoung
      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.

      [code=xml]
      <xsl:template match="template 1">
      <xsl:variable name="value" select="elem1/subelem1"/>
      <xsl:if test="$value &gt; 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=', name)"/>
      elem4 exists
      </xsl:if>
      </xsl:if>
      <xsl:template >
      [/code]

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        If you need to use the same template (subelem1) you can use <xsl:for-each ..\.. to change context to the template1 node:
        [code=xml]
        <xsl:template match="/root/template1/elem1/subelem1">
        <xsl:for-each select="..\..">
        all previous code
        [/code]

        Comment

        • njsimha
          New Member
          • Sep 2008
          • 17

          #5
          Hi young,
          Its working now.Thanks a lot.


          Originally posted by jkmyoung
          If you need to use the same template (subelem1) you can use <xsl:for-each ..\.. to change context to the template1 node:
          [code=xml]
          <xsl:template match="/root/template1/elem1/subelem1">
          <xsl:for-each select="..\..">
          all previous code
          [/code]

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I would have gone for:
            Code:
            <xsl:template match="//template1">
              <xsl:if test=".[elem2][elem1/subelem1 &amp;gt; 50]">
            ... error message here ...
              </xsl:if>
            </xsl:template>
            regards

            Comment

            Working...