Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on XSLT processing using Xpath

Tags:

xml

xslt

xpath

I have two simple templates listed below operating on some sample data also listed below:

<?xml version="1.0" encoding="utf-8?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="Gallery/Tab/ImageGroup/Image">
        <xsl:apply-templates  select="imageText" />
    </xsl:template>

    <xsl:template match="imageText">
        <h2><xsl:value-of select="." /></h2>
    </xsl:template>

 </xsl:stylesheet>


<?xml version="1.0" encoding="utf-8"?>

<Gallery name="My Gallery">

    <Tab tabID="imageDivTab0" height="500" name="Team Wear">
        <ImageGroup>
            <Image>
                <imageName>Challenge-Badge.jpg</imageName>
                <imageURL>images/gallery/small/Tab1/</imageURL>
                <imageText>Challenge Badge</imageText>
            </Image>
        </ImageGroup>
    </Tab>

</Gallery>

When the processor runs I get the expected result (imageText being displayed) if I comment out the first template I get everything being displayed (imageName, imageURL and image Text).

Is this because the second template is trying to match 'imageText' without the proper context, i.e. it is using the root node, so it displays everything.

I am fairly new to this so any help would be much appreciated.

Cheers

like image 921
ZosoOfZep Avatar asked Apr 06 '26 17:04

ZosoOfZep


1 Answers

There are several built-in template rules.

Reference: http://www.w3.org/TR/xslt#built-in-rule

In your case applied templates are:

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

and

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>
like image 181
Kirill Polishchuk Avatar answered Apr 08 '26 06:04

Kirill Polishchuk