Solved

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

Posted on 2011-02-18
7
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

624 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