Solved

EASY - help parsing an XML tree with XPath

Posted on 2011-03-21
9
351 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
[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
  • 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
Technology Partners: 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

718 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