• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

How to grab new node section via xsl ?

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
  • 3
  • 2
1 Solution
Geert BormansInformation ArchitectCommented:
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"

    <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 match="edt[@name='country']">
                <xsl:attribute name="name">ResultZone</xsl:attribute>
                <xsl:for-each select="ce">
                        <xsl:copy-of select="code"/>
                        <xsl:call-template name="GetZonesTemp">
                            <xsl:with-param name="country" select="code"/>
    <xsl:template name="GetZonesTemp">
        <xsl:param name="country"/>
                <xsl:when test="key('zone', $country)">
                <xsl:copy-of select="key('zone', $country)"/>
                    <xsl:value-of select="'Not  found'"/>

Open in new window

badtz7229Author Commented:
yes. that is excactly what i was looking for. thanks a lot.
Geert BormansInformation ArchitectCommented:
badtz7229Author Commented:
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.
Geert BormansInformation ArchitectCommented:
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


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now