Solved

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

Posted on 2011-02-18
7
373 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 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to page 4 86
The Best Website Creation Tools 14 56
Grunt Copy file to another destination. 1 37
what are list of ebay api errors 1 19
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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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