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

EASY - help parsing an XML tree with XPath

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
Emanuele_Ciriachi
Asked:
Emanuele_Ciriachi
  • 5
  • 4
1 Solution
 
Geert BormansCommented:
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
 
Emanuele_CiriachiAuthor Commented:
I will check this again tomorrow - I created the question when I was about to leave the office.
0
 
Emanuele_CiriachiAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Geert BormansCommented:
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
 
Emanuele_CiriachiAuthor Commented:
Thanks, I will try.
0
 
Geert BormansCommented:
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
 
Geert BormansCommented:
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
 
Emanuele_CiriachiAuthor Commented:
Thank you.
0
 
Geert BormansCommented:
welcome
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now