Solved

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

Posted on 2011-02-18
7
375 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import data into excel from web page 10 110
xpath sql query 2008 8 63
ASP AND XML 3 30
customizing windows 10 taskbar and startmenu 2 35
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

740 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