Solved

EASY - help parsing an XML tree with XPath

Posted on 2011-03-21
9
349 Views
Last Modified: 2013-11-11
Greetings - through proper usage of <xsl:template match="...."> I already managed to succesfully parse a number of nodes - that is, my XSL is correctly getting all the instances; let's say that one of these instances looks like this:

<label lang="en-GB">Contact</label>
<content>
	<Label id="ContactType">
		<label lang="en-GB">Contact Type</label>
	</Label>
	<Label id="Value">
		<label lang="en-GB">Value</label>
	</Label>
</content>

Open in new window


The questions is:

How can I parse, for each element, all Label instances? Is there a way of using match again to "reset" the current node, and/or cycle through all children of <content>?

Apologies if some of this makes little sense - I am quite new to XPath.
Feel free to ask more questions in order to get the informations you need to answer my question correctly.
0
Comment
Question by:Emanuele_Ciriachi
  • 5
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35183025
inside your template the match will define the "context"
you can continue processing the children
by using
<xsl:apply-templates/>
apply-templates will pass all teh child nodes of the current context to the templates
and an appropriate other template will pick up from there

so the <xsl:template match='label'>...
with do something with each label element
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 35184209
I will check this again tomorrow - I created the question when I was about to leave the office.
0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 35191253
Ok, this sort of worked, although in a different way from what you suggested.

I had already a template, registered with mode "html" that parsed the same elements found inside my object.
By invoking:

<xsl:apply-templates mode="html"/>

Open in new window


I was able to parse them again.
Is there a way to pass a parameter to this template?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 35191318
yes, by having an <xsl:with-param> child inside the xsl:apply-templates

just make sur you have
- either XSLT2 and use tunnel="yes"
- either have a template at every level in XSLT1, because parameters are not automatically tunnelled

I will show you the difference in the following examples
XML

<a>
    <b>
        <c>content</c>
    </b>
</a>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="a">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="c">
        <foo><xsl:value-of select="."/></foo>
    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Comment

by:Emanuele_Ciriachi
ID: 35191325
Thanks, I will try.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35191333
Now passing a parameter in XSLT2 using tunnel
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:template match="a">
        <xsl:apply-templates>
            <xsl:with-param name="name" select="'foo'" tunnel="yes"/>
        </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="c">
        <xsl:param name="name" tunnel="yes"/>
        <xsl:element name="{$name}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35191363
and adding a template for <b>, with the only purpose of serving as the tunnel in XSLT1

without the template for b, this will not work, test and see
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="a">
        <xsl:apply-templates>
            <xsl:with-param name="name" select="'foo'"/>
        </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="b">
        <xsl:param name="name"/>
        <xsl:apply-templates>
            <xsl:with-param name="name" select="$name"/>
        </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="c">
        <xsl:param name="name"/>
        <xsl:element name="{$name}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Closing Comment

by:Emanuele_Ciriachi
ID: 35197008
Thank you.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35197014
welcome
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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