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 12th, 2008, 05:14 PM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:for each problem

Hi

Have a problem with a loop of subitems. Having an xml input file containing a <servicelist> item, with several subitems <service>. The
service subitem contains four subelements, which can be seen in code below. But my problem lies in the loop repeating the first <service> item 'n' times, where 'n' corresponds to number of <service> items, which actually means the loop is repeated the right number of times.
The loop should pick the <service> items one by one, since in each round a new item should be the context. Putting position() in the loop gives the right response (1-'n').
Here's code from my xsl:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:na="http://www.netadmin.se/2006/API_XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
...
...
...
 <xsl:for-each select="//na:servicelist/na:service">
   <service>
      <service_id>
    <xsl:value-of select="//na:service_id"/>
      </service_id>
      <cpe_type>
    <xsl:value-of select="//na:cpe_type"/>
      </cpe_type>
      <state>
    <xsl:value-of select="//na:state"/>
      </state>
      <free_date>
    <xsl:value-of select="//na:free_date"/>
      </free_date>
   </service>
 </xsl:for-each>
</xsl:template
...
...
...
</xsl:stylesheet>
And here's the xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<GetAvailableServicesOut xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.netadmin.se/2006/API_XMLSchema" xsi:schemaLocation="http://www.netadmin.se/2006/API_XMLSchema C:\teliasonera\herkules\webServiceAdapter\XSD\GetAvailableServicesOut.xsd">
    <servicelist>
        <service>
            <service_id>4576</service_id>
            <cpe_type>Prod_cpe_type</cpe_type>
            <state>free</state>
            <free_date>2008-03-22</free_date>            
        </service>
        <service>
            <service_id>0123</service_id>
            <cpe_type>Test_cpe_type</cpe_type>
            <state>mod</state>
            <free_date>2008-03-12</free_date>
        </service>
        <service>
            <service_id>999</service_id>
            <cpe_type>Dev_cpe_type</cpe_type>
            <state>dev</state>
            <free_date>2009-03-22</free_date>            
        </service>
    </servicelist>
    <reply>
        <err_number>0</err_number>
        <err_description>a</err_description>
    </reply>
</GetAvailableServicesOut>
Any of you have a clue what's causing this problem???

/R

 
Old March 12th, 2008, 06:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:value-of select="//na:service_id"/>

Get rid of that "//" - it causes selection from the root of the document, whereas you want selection underneath the context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 13th, 2008, 12:54 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Mhkay

Tried it, with some modification, and now it works fine
When removing the double slash from for each, there was no iteration in loop, but this works:
<xsl:for-each select="//na:servicelist/na:service">

while removing '//' from value-of expressions made it
Does this makes sense?

Thanks again

/R

 
Old March 13th, 2008, 04:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It makes perfect sense when you understand what // actually does.

//na:service_id will select every single na:service_id element in the entire document, therefore <xsl:value-of select="//na:service_id"/> will always select the text value of the first element in the document.

When you are inside the xsl:for-each instruction the 'context' as it is called has changed, so you are now positioned at each service element in turn - so you you don't want to then select every single service_id element, you want the service_id element which is the child of the current service element.

To select something on the 'child' axis you can use the following convention: "child::na:service_id" - however in XSLT the child axis is the default axis, so can be removed and you can just use "na:service_id"

I'd recommend getting a good book on the subject if you plan to do much work with XSLT as you obviously need to get a handle on the basics.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 13th, 2008, 04:45 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot

Of course it makes sense to me, reading your answer
And it's a good idea to dig deeper into this, since there's more of this job coming up

Worked a lot years ago, and memory is good, but too short :-)

Appreciate your help very much

Regards

/R
 
Old March 13th, 2008, 04:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Question is, do you understand why it now works?

It's crucial to understand the notion of context and its effect on the values of path expressions.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 13th, 2008, 06:11 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, I do, but to set this permanently I need to refresh and reinforce my knowledge in this. Worked a lot with XSLT-FO and PDF generation years ago, and that sure was a struggle with both XPATH and contexts, but also with positioning, but you tend to forget things you leave behind... And that's why these boards are so valuable, and of course other resources such as litterature, walk-thru's and so on

And I really like the commitment to help people here

Cheers

/R






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL for-each/when problem Grofit XSLT 11 March 27th, 2008 10:26 AM
XSL problem cheez XSLT 1 September 14th, 2006 01:20 AM
Problem with XSL sandeepa XSLT 5 May 13th, 2005 04:31 AM
Help with xsl:when problem sundar_revathi XSLT 2 November 9th, 2004 07:39 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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