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 August 25th, 2007, 10:23 AM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Joining two node sets

Hello - I've got a doozy of an issue that I've been trying to work out for a couple days now (in the back of my head). I'd say I am a novice XSLT programmer. Here is a sample of the xml that I am transforming (i will only include a few important nodes):

<root>
    <Sections>
        <Entity>
            <EntityID/>183<EntityID>
            <Description/>description</Description>
        </Entity>
        <Entity>
            <EntityID/>184<EntityID>
            <Description/>description</Description>
        </Entity>
    </Sections>
    <SectionCounts>
        <Entity>
            <EntityID/>183<EntityID>
            <Count/>0</Count>
        </Entity>
        <Entity>
            <EntityID/>184<EntityID>
            <Count/>2</Count>
        </Entity>
    </SectionCounts>
</root>

I need to query out all /root/Section/Entities that have corresponding /root/SectionCounts/Entity/Count > 0 and then I need to do a for-each loop on them. After performing this join I need to run further xslt to format output. I can only run one transform and cannot change the given xml.

I tried to make this concise. If my description of the problem is not clear, please let me know.
Any advice is appreciated. Thanks!

 
Old August 25th, 2007, 11:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've got a bit muddled with your XML syntax - "/>" used in various inappropriate places.

You can do

<xsl:variable name="Sections" select="/root/Sections"/>
<xsl:variable name="SectionCounts" select="/root/SectionCounts"/>
<xsl:for-each select="$Sections/Entity[EntityId = $SectionCounts[Count != 0]/EntityId]">
  ...
</xsl:for-each>

You asked for an xsl:for-each solution, so I gave you one, but if you had a little more experience you would probably have asked for xsl:apply-templates, to make your code more modular.

You don't need either of the variables here, they are just for readability.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 25th, 2007, 05:37 PM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This close but there is a bit of a problem. There may be more than one /root/SectionCounts/Entity with a count greater than zero. I will expand the set a bit to demonstrate the issue.

<root>
    <Sections>
        <Entity>
            <EntityID>183</EntityID>
            <Description>description</Description>
        </Entity>
        <Entity>
            <EntityID>184</EntityID>
            <Description>description</Description>
        </Entity>
        <Entity>
            <EntityID>185</EntityID>
            <Description>description</Description>
        </Entity>
    </Sections>
    <SectionCounts>
        <Entity>
            <EntityID>185</EntityID>
            <Count>9</Count>
        </Entity>
        <Entity>
            <EntityID>183<EntityID>
            <Count>0</Count>
        </Entity>
        <Entity>
            <EntityID>184</EntityID>
            <Count>2</Count>
        </Entity>
    </SectionCounts>
</root>

The test you offered before will return 185 every time and the 184 node will not be included in the for each loop. Any suggestions?

I feel quite silly for the malformed XML.
 
Old August 26th, 2007, 07:39 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, sorry I left out a step in the path, it should be

<xsl:variable name="Sections" select="/root/Sections"/>
<xsl:variable name="SectionCounts" select="/root/SectionCounts"/>
<xsl:for-each select="$Sections/Entity[EntityId = $SectionCounts/Entity[Count != 0]/EntityId]">
  ...
</xsl:for-each>

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

I believe that the right hand side of the outer predicate ($SectionCounts/Entity[Count != 0]/EntityId]) will always return the first node in its node set. For this reason, this for-each statement will return one node at the most. What I really need is something like this:
<xsl:for-each select="$Sections/Entity[EntityId [u]IN</u> $SectionCounts/Entity[Count != 0]/EntityId]">

Am I missing something? Please clarify.

 
Old August 27th, 2007, 11:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I believe that the right hand side of the outer predicate ($SectionCounts/Entity[Count != 0]/EntityId]) will always return the first node in its node set.

You believe wrong.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 27th, 2007, 04:03 PM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh goodness, it worked! Thanks very much.






Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Correlation sets jsfitter Biztalk 0 March 4th, 2005 12:03 PM
Creating & comparing node sets skipw XSLT 3 February 21st, 2004 05:51 AM





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