INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XSLT not following path in for-each

XSLT not following path in for-each

(OP)
I need to go back up a level (or 2) when in a for-each loop.  The loop works fine when using name() and "." but doesn't seem to work for "../@something".

If you look in my example you will see "../@ID" before the for-each and after the for-each.  I get a value for the first one but not the second.

CODE

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="METHODS/COMPARISON">
        <xsl:with-param name="reportName" select="REPORT/@MajorFormType"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- Now we handle the other sections after the DATA section -->

  <xsl:template match="COMPARISON">
    <xsl:param name="reportName"/>
    <xsl:apply-templates>
      <xsl:with-param name="reportName" select="$reportName"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="SALE">
    <xsl:param name="reportName"/>
    <xsl:value-of select="../@ID"/>    <!-- Works Here -->
    <xsl:for-each select="@*">
      <form>
        <ID>
          <xsl:value-of select="../@ID"/>  <!-- Does Not Work Here -->
        </ID>
        <section>
          <xsl:text>0</xsl:text>
        </section>
        <formName>
          <xsl:value-of select="$reportName"/>
        </formName>
        <tagName>
          <xsl:value-of select="name()"/>
        </tagName>
        <value>
          <xsl:value-of select="."/>
        </value>
      </form>
    </xsl:for-each>
    <xsl:apply-templates>
      <xsl:with-param name="reportName" select="$reportName"/>
    </xsl:apply-templates>
  </xsl:template>
 </xsl:stylesheet>

If you look at the results you will see "241" which is the value before the for-each and 3 <ID></ID>.  These should show as <ID>241</ID>.

CODE

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
  241
  <form>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>SequenceId</tagName>
    <value>0</value>
  </form><form>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>state</tagName>
    <value>true</value>
  </form><form>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>saleDate</tagName>
    <value>05/15/2011</value>
  </form>
</RESPONSE>

Why does this not work?

Thanks,

Tom

RE: XSLT not following path in for-each

(OP)
I found I can set up a variable and it would work inside the for-each.  But I would rather access it directly.

Here is the changed code where I set up a variable "temp":

CODE

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="METHODS/COMPARISON">
        <xsl:with-param name="reportName" select="REPORT/@MajorFormType"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- Now we handle the other sections after the DATA section -->

  <xsl:template match="COMPARISON">
    <xsl:param name="reportName"/>
    <xsl:apply-templates>
      <xsl:with-param name="reportName" select="$reportName"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="SALE">
    <xsl:param name="reportName"/>
    <xsl:variable name="temp" select="../@ID"/>
    <xsl:value-of select="../@ID"/>    <!-- Works Here -->
    <xsl:for-each select="@*">
      <form>
        <var>
          <xsl:value-of select="$temp"/>
        </var>
        <ID>
          <xsl:value-of select="../@ID"/>  <!-- Does Not Work Here -->
        </ID>
        <section>
          <xsl:text>0</xsl:text>
        </section>
        <formName>
          <xsl:value-of select="$reportName"/>
        </formName>
        <tagName>
          <xsl:value-of select="name()"/>
        </tagName>
        <value>
          <xsl:value-of select="."/>
        </value>
      </form>
    </xsl:for-each>
    <xsl:apply-templates>
      <xsl:with-param name="reportName" select="$reportName"/>
    </xsl:apply-templates>
  </xsl:template>
 </xsl:stylesheet>

Here is the result I get from this:

CODE

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
  241
    <form>
    <var>241</var>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>SequenceId</tagName>
    <value>0</value>
  </form><form>
    <var>241</var>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>state</tagName>
    <value>true</value>
  </form><form>
    <var>241</var>
    <ID></ID>
    <section>0</section>
    <formName>Form102</formName>
    <tagName>saleDate</tagName>
    <value>05/15/2011</value>
  </form>
</RESPONSE>

Thanks,

Tom

RE: XSLT not following path in for-each

The for-each changes the context node.  So, your Xpath expression must adjust accordingly.  Since the context inside the for-each is an attribute node, ../@ID is looking for sibling attribute (on the SALE node because the .. gets you back to the SALE node), not for an attribute on the parent of SALE.  Try ../../@ID and see what happens.

Tom Morrison
Micro Focus

RE: XSLT not following path in for-each

(OP)
That was it.

Thanks,

Tom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close