Solved

EASY - help parsing an XML tree with XPath

Posted on 2011-03-21
9
341 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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, …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now