Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2007, 04:27 AM
Registered User
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL Loop and incrementing a variable

We are using Oracle XML Publisher to generate PDF formatted Purchase Order Reports. We have tried Oracle Support, but haven't got anywhere, hence trying here...

As part of this work, we have hit a problem.

Part of the XML contains the following data:
Code:
<PO_DATA>
    <LINE_ATTACHMENTS>
        <TEXT> Telephone Number=6955_1 </TEXT> 
        <ID>765923</ID> 
        <TEXT> Telephone Number=6955_2 </TEXT> 
        <ID>765924</ID> 
        <TEXT>Telephone Number=6955_3</TEXT> 
        <ID>765925</ID> 
    </LINE_ATTACHMENTS> 
</PO_DATA>
What we want to do is to loop through the values in the <LINE_ATTACHMENTS> tag, and output the Text Value.

If we try the following snippet:
Code:
<?/PO_DATA/LINE_ATTACHMENTS/TEXT[3]?>
Then when we view the output, the XSL will always display the value of the 3rd iteration of the <TEXT> tag.

That's okay - but we don't want to hard code the [3] value. We want instead for the number there to be the row count that is currently being used.

We have tried this:
Code:
<?/PO_DATA/LINE_ATTACHMENTS/TEXT["row:position()]?>
But a long error message was returned.

Basically, we want to increment a variable for each loop through the data, and output the value of that incremented variable in the square brackets as detailed above.

Can this be done?

Thanks

Jim
 
Old March 29th, 2007, 05:34 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Your terminology is wrong which makes it difficult to answer, you can't iterate/loop in XSLT. Do you want to show the last TEXT element in LINE_ATTACHMENTS? In that case PO_DATA/LINE_ATTACHMENTS/TEXT[last()] should do. Otherwise please explain what you mean by "row count that is currently being used".

--

Joe (Microsoft MVP - XML)
 
Old March 29th, 2007, 05:51 AM
Registered User
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for my unclear explanation.

What is happening is that we are using an XML document which contains the raw data for the PO (PO header, order lines etc.).

Oracle XML Publisher uses a Word Template to create a report formatted as a PDF document. The output of this is an xsl file which formats the XML data correctly.

As part of that, there is a Group By statement which loops through the po order lines to show the line level information held of the Purchase Order.

So when I said "row count that is currently being used" what I should have said is to return the number of the row being looped through via the group statement.

Last() would be okay - but then using the XML snipped I pasted here, it would only ever return the last value, and then repeats it three times.

Thanks

Jim

 
Old March 29th, 2007, 06:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sounds like you need the position() function, can you show more of XSL?

--

Joe (Microsoft MVP - XML)
 
Old March 29th, 2007, 06:28 AM
Registered User
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

this is a snippet of the XSL - tried position() but it only returns the first of the text values, and repeats it 3 times:

Code:
- <fo:block xdofo:xliff-note="TEXT" xdofo:use-attribute-sets="b_42 b_1">
- <fo:inline xdofo:use-attribute-sets="i_27">
  <xsl:value-of select="/PO_DATA/LINE_ATTACHMENTS/TEXT[position()]" xdofo:field-name="/PO_DATA/LINE_ATTACHMENTS/TEXT[position()]" /> 
  </fo:inline>
  </fo:block>
It looks close - but not close enough!

Thanks

Jim

 
Old March 29th, 2007, 03:52 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi,

You say:

What we want to do is to loop through the values in the <LINE_ATTACHMENTS> tag, and output the Text Value.

but your code asked for the position:

<xsl:value-of select="/PO_DATA/LINE_ATTACHMENTS/TEXT[position()]"

if you want the values in the <LINE_ATTACHMENTS> tag, just use:

<xsl:value-of select="/PO_DATA/LINE_ATTACHMENTS/TEXT"/>
 
Old March 29th, 2007, 04:10 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

position() determines the position of the context item in the list of items currently being processed. Within [], the context item refers to the item being tested, and the "list of items being processed" is the set of nodes you are filtering. Looking at it another way, using an integer value $X within square brackets [$X] is short for [position()=$X], so [position()] is short for [position()=position()] which matches every node. You need to bind a variable to the value of position() outside the predicate, and then use the variable within the square brackets.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 30th, 2007, 10:01 AM
Registered User
 
Join Date: Mar 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the replies,

the last one made sense - so, I tried this in XML Publisher:

<xsl:variable name="line" select="position()" />
<?/PO_DATA/LINE_ATTACHMENTS/TEXT[$line]?>

Still, it only ever displays the first value of the text tag from line_attachments, and repeats it 3 times.

When I look at the resulting xsl document that XML publisher creates, the variable declaration is right at the top of the xsl document:

<xsl:variable xdofo:ctx="8" name="line" select="position()" />

while the part referencing line_attachments is much further down:

<xsl:value-of select="/PO_DATA/LINE_ATTACHMENTS/TEXT[$line]" xdofo:field-name="/PO_DATA/LINE_ATTACHMENTS/TEXT[$line]" />

I'm not sure where to go from here. I have asked Oracle support for advice, but they haven't advised!

Sorry if I'm not making a lot of sense here.

Thanks

Jim

 
Old March 30th, 2007, 10:41 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Maybe I am missing something...why are you asking for the position()?
I thought you wanted all the values (i.e. "What we want to do is to
loop through the values in the <LINE_ATTACHMENTS> tag, and output the Text Value."

Is this your intension?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 04:43 PM
Way to incrementing value of variable in xsl vikkiefd XSLT 14 March 12th, 2008 10:33 PM
For Loop Not incrementing donrafeal7 Javascript 1 October 24th, 2006 12:24 AM
incrementing javascript loop use i++ or ++i crmpicco Javascript How-To 4 May 11th, 2006 01:39 AM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 12:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.