Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to grab found parent node and only its relevant child nodes

Posted on 2011-02-18
7
Medium Priority
?
393 Views
Last Modified: 2013-11-08
I have xsl with help from EE that searches for a country within another node and if found returns the parent and all of its contents
however, the parent contains many sub-zones, i dont want to return the sub-zones that don't contain the country i was initially searching for.
please assist.
input.xml
Output.xml
test1.xsl
0
Comment
Question by:badtz7229
  • 4
  • 3
7 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 34931741
It all depends on how deep you index.
Is this what you are looking for?
<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version = "1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:key name="sub-zone" match="edt[@name = 'zones']/ce/data/zone/sub-zone" use="locations/location/@code"/>
    
    <xsl:template match = "/">
        <xsl:apply-templates select="ROOT/edt[@name='country']"/>
    </xsl:template>
    
    
    <xsl:template match="edt[@name='country']">
        <edt>
            <xsl:attribute name="name">ResultZone</xsl:attribute>
            <xsl:for-each select="ce">
                <ce>
                    <xsl:copy-of select="code"/>
                    <xsl:call-template name="GetZonesTemp">
                        <xsl:with-param name="country" select="code"/>
                    </xsl:call-template>
                </ce>
            </xsl:for-each>
        </edt>
        
    </xsl:template>
    
    
    <xsl:template name="GetZonesTemp">
        <xsl:param name="country"/>
        <desc>
            <xsl:choose>
                <xsl:when test="key('sub-zone', $country)">
                    <zone>
                        <xsl:copy-of select="key('sub-zone', $country)/ancestor::zone[1]/@*"/>
                        <xsl:copy-of select="key('sub-zone', $country)"/>
                    </zone>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'Not  found'"/>
                </xsl:otherwise>
            </xsl:choose>          
        </desc>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:badtz7229
ID: 34943218
Gertone:
Can you please explain why in the GetZonesTemp template you do a copy-of select 2x?

 <xsl:copy-of select="key('sub-zone', $country)/ancestor::zone[1]/@*"/>
<xsl:copy-of select="key('sub-zone', $country)"/>


the result is correct, but I'd like to understand what this ancestor feature does and why you wouldn't get dups given that you copy-select twice
0
 

Author Comment

by:badtz7229
ID: 34943250
Gertone:

i also wanted to ask how would you resolve the issue where the country i was searching for exists in both sub-zones, but i only want the 2nd instance of it?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34943717
copy-of makes a deep copy of the selected node(set)

<xsl:copy-of select="key('sub-zone', $country)"/> copies the sub-zone which I get back from using the key() function

<xsl:copy-of select="key('sub-zone', $country)/ancestor::zone[1]/@*"/> copies a different node. It selects the same sub-zone to start with, travels to the ancestor (that are the elements higher up the hierrachy) with the name "zone", takes the first one it encounters, and then selects all its attributes

So the first copy-of in the xslt copies the attributes of the parent, the second copy-of copies the entire sub-zone
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 2000 total points
ID: 34943728
<xsl:copy-of select="key('sub-zone', $country)[last()]"/>
would select the last sub-zone, being the first if there is only one, or the second if there are two
would that help?
0
 

Author Comment

by:badtz7229
ID: 34944618
Gertone:
yes  your last code snippet definetly worked.
can you please explain how? (i'm a newbie to xsl, so am learning as i go)
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34944751
key('sub-zone', $country) will return all subzones
[] adds a predicate in which you can define some restrictions
last() is short for position() = last()

key('sub-zone', $country)[last()]
will select the last sub-zone from teh selected sub-zones
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

972 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