Solved

How to grab new node section via xsl ?

Posted on 2011-02-18
5
432 Views
Last Modified: 2013-11-08
I have an xsl which reads 1 xml file that contains 2 differently named nodes. This xsl grabs the value of an element from 1st node and compares it to that of the attribute from 2nd node.
If found I want to retrieve the entire node from 2nd node. Not just the attribute or element containing the attribute. But also the parent node.
Please advise on proper syntax for this.

I've attached input.xml and test.xsl
test.xsl
input.xml
0
Comment
Question by:badtz7229
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 34926502
This seems to be what you want
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

    <xsl:output indent="yes"/>
    <xsl:key name="zone" match="edt[@name = 'zones']/ce/data/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('zone', $country)">
                <xsl:copy-of select="key('zone', $country)"/>
                </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 Closing Comment

by:badtz7229
ID: 34926641
yes. that is excactly what i was looking for. thanks a lot.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34926665
welcome
0
 

Author Comment

by:badtz7229
ID: 34927064
Gertone:
i have another quick question. not sure if i can ask after i've closed this tix, but here goes anyway:

What if the input file is now one which contains various <locations> within a <sub-zone> element and I only want to return node of that 1 sub-zone that matches ?

Right now with your help, my resulting xml returns the matching zone with matching locations alongside all the other locations as well.  i want to reduce it to return only matching records not everything else included in the child node.

see attached input file and output file.
input.xml
Output.xml
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34931748
I just saw your follow up question en answered there.
I don't have a problem to continue following up on questions, but I was out yesterday evening
and posting a new questions has the benefit of potential other experts to step in
and gives you a faster return

cheers

Geert
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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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…
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)

777 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