user3041630
user3041630

Reputation: 3

XSL template match not working

I have a XML document :

<Chart>
    <ChartAreas>
        <ChartArea>
           <ChartValueAxes>
                <ChartAxis>
                    <Style>
                        <Border>
                            <Color>Tan</Color>
                         </Border>
                         <FontFamily>Arial Narrow</FontFamily>
                         <FontSize>16pt</FontSize>
                    </Style>
                </ChartAxis>
           </ChartValueAxes>
        </ChartArea>
   </ChartAreas>
</Chart>

I have two template match statements as I want the Style/Border element processed by TemplateA and everything else under Style processed by TemplateB. However everything is being processed by TemplateB.

<xsl:template match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style/Border" >
        <xsl:call-template name="TemplateA"/>
</xsl:template>

<xsl:template match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style" >
     <xsl:call-template name="TemplateB"/>
</xsl:template>

Upvotes: 0

Views: 1505

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122424

You have a template that matches the Style element itself and calls TemplateB. Therefore (unless TemplateB does so explicitly) nothing is causing templates to be applied to the children of Style, so the Border template never fires.

I want the Style/Border element processed by TemplateA and everything else under Style processed by TemplateB

In that case your templates should be

<xsl:template priority="10"
    match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style/Border" >
        <xsl:call-template name="TemplateA"/>
</xsl:template>

<xsl:template priority="5"
    match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style/*" >
        <xsl:call-template name="TemplateB"/>
</xsl:template>

(I've used explicit priorities because both these rules could apply to the Border element and they have the same default priority)

You can shorten the match expressions, e.g. match="Style/*" - you don't need the full path as there are no other Style elements elsewhere that might confuse things.

But even simpler would just be to remove the call-template and put the match expressions on TemplateA and TemplateB directly - a template can have both a name and a match

<xsl:template match="Style/Border" name="TemplateA" priority="10">
        <!-- content of template A -->
</xsl:template>

<xsl:template match="Style/*" name="TemplateB" priority="5">
        <!-- content of template B -->
</xsl:template>

Upvotes: 2

Erlock
Erlock

Reputation: 1968

Use mutually exclusives match statements:

<xsl:template match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style/Border" >
        <xsl:call-template name="TemplateA"/>
</xsl:template>

<xsl:template match="Chart/ChartAreas/ChartArea/ChartValueAxes/ChartAxis/Style/*[not(self::Border])]" >
     <xsl:call-template name="TemplateB"/>
</xsl:template>

Upvotes: 1

Related Questions