?
Solved

EASY - help parsing an XML tree with XPath

Posted on 2011-03-21
9
Medium Priority
?
352 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 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