Solved

XSLT/XPATH - link ancestor node value

Posted on 2008-06-15
10
1,019 Views
Last Modified: 2013-11-18
Code of interest:
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn=ancester::Fitemn*]" order-by="@SeqNo">

In my code sample, I am trying to make the above line of code to associate between the //WebformData/Itlist/Fitemn  and  //WebformData/Itlist/Dilist/Fitemn
There is a one to many relationship between //WebformData/Itlist and  //WebformData/Itlist/Dilist with the Fitemn being the link.

When I substitute the following code, the data shows up correctly
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn=1]" order-by="@SeqNo">
The following code also works
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn=2]" order-by="@SeqNo">

So, it looks like what I need is the code that properly references the ancestor Fitemn node with the self Fitemn node.

Thanks for any help.  I am learning XSLT and XPATH and very stuck!

Keith
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="JavaScript">

<xsl:script>
 

function spaces(number)

{

        var string = "";

        var blank = " ";

        while (string.length &lt; number)

        {

                string = string + blank;

        }

        return string;

}
 

function PadString(value, number)

{

        var string = "";

        var blank = " ";

        

        if (value.text.length > 0)

           string = value.text;

           

        while (string.length &lt; number)

        {

                string = string + blank;

        }

        

        return string;          

}
 

function PadNumeric(value, number)

{

        var string = "";

        var blank = " ";

        

        if (value.text.length > 0)

           string = value.text;

           

        while (string.length &lt; number)

        {

                string = blank + string;

        }

        

        return string;          

}
 

</xsl:script>

<xsl:template match="/">

<table border="1">

<tr><th>PR#</th><th>SEC CODE</th></tr>

<tr><td>

<xsl:for-each select="xml/user_vars/pr"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/seccd"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

</table>
 

<table border="1">

<tr><td>Is this a purchase of goods/materials</td>

<td>

<xsl:for-each select="xml/user_vars/puryn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr><tr>

<td></td>

<td>

<xsl:for-each select="xml/user_vars/qtyyn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

<tr>

<td>Is this a service agreement or a contract?</td>

<td>

<xsl:for-each select="xml/user_vars/agryn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

<tr>

<td>

Is insurance on file?

</td>

<td>

<xsl:for-each select="xml/user_vars/propyn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>
 

<tr>

<td>Is there a proposalNoYes

</td>

<td>

<xsl:for-each select="xml/user_vars/insyn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>
 

</tr>
 

</table>

   

<table border="1">

<tr><th>DATE REQUESTED</th><th>END USE/DEPT</th><th>ENTERED BY</th><th>ENTER DATE </th></tr>

<tr>

<td>

<xsl:for-each select="xml/user_vars/reqdt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/enduse"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/entby"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/entdt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

</table>

<table border="1">

<tr><th>DATE EXPECTED</th><th>DATE REQUIRED</th><th>DELIVERY DATE</th><th>EXPIRATION DATE </th></tr>

<tr>

<td>

<xsl:for-each select="xml/user_vars/expdt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/reddt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/deldt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/exprdt"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

</table>

   
 

<table border="1">

<tr><th>VENDOR ID</th><th>VENDOR NAME</th><th>VEN ADDR CD</th><th>VENDOR ADDRESS  </th></tr>

<tr>

<td>

<xsl:for-each select="xml/user_vars/venid"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/vennam"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

<xsl:for-each select="xml/user_vars/venph"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

<xsl:for-each select="xml/user_vars/venfx"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/vencd"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

<td>

<xsl:for-each select="xml/user_vars/venad"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each>

</td>

</tr>

</table>
 
 

<table border="1">

<tr><th>Item</th><th>Qty</th><th>Amt</th><th>Unit</th><th>Tax Cd</th><th>Ext Amt</th><th>Desc</th><th>Key</th><th>Obj</th></tr>
 

<xsl:for-each select="//WebformData/Itlist" order-by="@SeqNo">
 

<tr>

<td valign="top">

<xsl:for-each select="Fitemn"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Fquant"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Festam"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Funit"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Ftaxcd"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Fextpr"><xsl:value-of select="."></xsl:value-of></xsl:for-each>

</td>

<td valign="top">

<xsl:for-each select="Fdescr"><xsl:value-of select="."></xsl:value-of><table>

<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn=ancester::Fitemn*]" order-by="@SeqNo"><tr>

<td><xsl:for-each select="Fperc"><xsl:value-of select="."></xsl:value-of></xsl:for-each></td>

<td><xsl:for-each select="Fobjcd"><xsl:value-of select="."></xsl:value-of></xsl:for-each></td>

<td><xsl:for-each select="Forgky"><xsl:value-of select="."></xsl:value-of></xsl:for-each></td>

</tr></xsl:for-each></table>

</xsl:for-each>

</td>

<td valign="top">

<xsl:for-each select="Forgky"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

<td valign="top">

<xsl:for-each select="Fobjcd"><xsl:value-of select="."></xsl:value-of>

</xsl:for-each></td>

</tr>
 

</xsl:for-each>

</table>
 
 
 
 

</xsl:template>

</xsl:stylesheet>

Open in new window

0
Comment
Question by:KeithMcElroy
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:KeithMcElroy
ID: 21790447
Here is a sample of the xml
- <xml>

- <LastLog>

- <Log>

  <Entry Level="8" Type="W" ID="Main Flow">Start</Entry> 

  <Entry Level="4" Type="T" ID="T4" Result="-1" /> 

  <Entry Level="4" Type="A" ID="A1">Transition T4 is true</Entry> 

  <Entry Level="4" Type="A" ID="A1" Result="-1">At least one Transition is active</Entry> 

  <Entry Level="4" Type="A" ID="A1" Result="2">Execute Results</Entry> 

  <Entry Level="4" Type="A" ID="A4" Result="5">Activity in Future Transition</Entry> 

  </Log>

  </LastLog>

- <WebformData>

- <Itlist SeqNo="1">

  <Fitemn>1</Fitemn> 

  <Fquant>100</Fquant> 

  <Festam>100</Festam> 

  <Funit>BOX</Funit> 

  <Ftaxcd>7.25</Ftaxcd> 

  <Fextpr>10725.00</Fextpr> 

  <Fdescr>PENCILS</Fdescr> 

  <Forgky>03321</Forgky> 

  <Fobjcd>5011</Fobjcd> 

  <Fpronu /> 

  <Factcd /> 

- <Dilist SeqNo="1">

  <Fdistn>1</Fdistn> 

  <Fitemn>1</Fitemn> 

  <Forgky>03321</Forgky> 

  <Facct /> 

  <Fproj /> 

  <Fobjcd>5011</Fobjcd> 

  <Fperc>90</Fperc> 

  <Famt>10000</Famt> 

  </Dilist>

- <Dilist SeqNo="2">

  <Fdistn>2</Fdistn> 

  <Fitemn>1</Fitemn> 

  <Forgky>00503</Forgky> 

  <Facct /> 

  <Fproj /> 

  <Fobjcd>1200</Fobjcd> 

  <Fperc>10</Fperc> 

  <Famt>725</Famt> 

  </Dilist>

  </Itlist>

- <Itlist SeqNo="2">

  <Fitemn>2</Fitemn> 

  <Fquant>100</Fquant> 

  <Festam>100</Festam> 

  <Funit>BOX</Funit> 

  <Ftaxcd>7.25</Ftaxcd> 

  <Fextpr>10725.00</Fextpr> 

  <Fdescr>RULERS</Fdescr> 

  <Forgky>03321</Forgky> 

  <Fobjcd>5011</Fobjcd> 

  <Fpronu /> 

  <Factcd /> 

- <Dilist SeqNo="1">

  <Fdistn>1</Fdistn> 

  <Fitemn>2</Fitemn> 

  <Forgky>03321</Forgky> 

  <Facct /> 

  <Fproj /> 

  <Fobjcd>5011</Fobjcd> 

  <Fperc>80</Fperc> 

  <Famt>9000</Famt> 

  </Dilist>

- <Dilist SeqNo="2">

  <Fdistn>2</Fdistn> 

  <Fitemn>2</Fitemn> 

  <Forgky>00503</Forgky> 

  <Facct /> 

  <Fproj /> 

  <Fobjcd>1200</Fobjcd> 

  <Fperc>20</Fperc> 

  <Famt>1725</Famt> 

  </Dilist>

  </Itlist>

  </WebformData>

- <user_vars>

  <agryn>No</agryn> 

  <appcd /> 

  <appdt /> 

  <appr1 /> 

  <appr2 /> 

  <appr3 /> 

  <comments>03321 501100503 1200</comments> 

  <comments_1 /> 

  <deldt>7/1/2008</deldt> 

  <divis /> 

  <enduse>TEST END USE</enduse> 

  <entby>BSI</entby> 

  <estup /> 

  <expdt>7/1/2008</expdt> 

  <exprdt /> 

  <insyn>No</insyn> 

  <next /> 

  <ponum /> 

  <pr>TEST0617</pr> 

  <propyn>No</propyn> 

  <puryn>No</puryn> 

  <qtyn /> 

  <reddt>7/1/2008</reddt> 

  <req /> 

  <reqdt>7/1/2008</reqdt> 

  <reqnm>KEITH TESTING</reqnm> 

  <reqno /> 

  <seccd>@@</seccd> 

  <venad>P O BOX 99023</venad> 

  <venaddc /> 

  <vencd>R1</vencd> 

  <venid>V01223</venid> 

  <vennam>my vendor</vennam> 

  <verif /> 

  </user_vars>

  <OldData /> 

  </xml>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 21791079
You are using an old working draft of XSLT, that is only supported by microsoft, and that has been dropped 10 years ago
xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"
you should use real XSLT instead
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

I think what you want to do is this
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn = ancestor::Itlist/Fitemn]" order-by="@SeqNo">
0
 

Author Comment

by:KeithMcElroy
ID: 21794130
1.  I switched to the suggested XSLT first without any other modifications.
Result:  Style sheet does not work.  
Note: This is a financial application that was built approximately 1998-2000.  That would explain why it is so old.

2.  I dropped in the new XPATH with ancestor code, both with and without #1 above.
Result: Style sheet does not work, with old or new XSLT.

Any ideas on next steps?  Is the XPATH ancestor code dependent on #1 above?
Are there any ideas on why the application is tied to the older XSLT?

Thank you for all the help,
Keith

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 21794368
It could well be that ancestor was not supported in the working draft, I don't remember.
You could try to make this a variable and compare to the variable...
but I am very unaware of Working Draft syntax for that

I used the working draft only because it was the first XSLT implementation.
I threw it out of the window asap when the real thing arrived :-)
0
 
LVL 12

Expert Comment

by:jkmyoung
ID: 21797319
Instead of using <xsl:script> elements (which I think are deprecated), I suggest writing the javascript in the header like so:

<xsl:template match="/">
<script type="text/javascript">
.... contents of <xsl:script> elements. ...
</script>
rest of your xsl
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:KeithMcElroy
ID: 21806751
jkmyoung:  OK, I think I understand. Actually, I think I can get rid of the javascript.  It was a copy over from another page and I believe does not apply in this page.  Is your thought that the javascript was the point of failure with the newer XLS and that I will be able to use the newer XLS (after dropping the javascript which is deprecated) and thus use the ancestor solution?  I will try this and advise.  Thank you!
Keith
0
 

Author Comment

by:KeithMcElroy
ID: 21809814
Did not work.  I stripped out the javascript and replaced the declaration.
Any alternatives?  
0
 

Author Comment

by:KeithMcElroy
ID: 21819370
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn = //WebformData/Itlist/Fitemn]" order-by="@SeqNo"><tr>

Is there a way to make this statement reference the current node?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 21819430
would this not work?
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn = current()/Fitemn]" order-by="@SeqNo"><tr>

though I am afraid current() is not supported in Working Draft either
0
 
LVL 12

Accepted Solution

by:
jkmyoung earned 500 total points
ID: 21842687
To avoid using current, you create a variable to the Fitemn value, and use it in your for each.
Eg, depending on context:
<xsl:variable name="Fitemn" select="Fitemn"/>
<xsl:for-each select="//WebformData/Itlist/Dilist[Fitemn = $Fitemn]">

or perhaps:

<xsl:for-each select="//WebformData/Itlist">
  <xsl:variable name="Fitemn" select="Fitemn"/>
  <xsl:for-each select="Dilist[Fitemn = $Fitemn]">
   ......
  </xsl:for-each>
</xsl:for-each>

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET and XML parsing 6 64
Having Returning  Anonymous Function Access Global Function 7 80
Grunt No Clean Targets 6 105
Problem to picture file 3 67
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now