Solved

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

Posted on 2011-02-18
7
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now