1

I have a XSL file which does the matching of template rules in XSLT 2.0 just to check which template rule has higher priority. But it only goes to default one and not to others even if priority is set.You can see it live here: http://xsltransform.net/nc4NzQ5/4 My XSL file is schema-aware.

Following is my XML:

  <test>test</test>
  <test attr1="1">test2</test>
  <test attr1="2">test3</test>
  <test attr3="4">test4</test>
  <test attr4="4">test5</test>
  <test attr5="3">test6</test>

XSL file is:

<xsl:template match="doc">
        <out>
         <xsl:apply-templates select="*"/>
        </out>
</xsl:template>

      <xsl:template match="test"><match>test</match></xsl:template>
      <xsl:template match="element(test)[attr1='1']"><match>element(test)[attr1='1']</match></xsl:template>
      <xsl:template match="element(test)[attr1='2']"><match>element(test)[attr1='2']</match></xsl:template>
      <xsl:template match="element(test)[attr3='4']"><match>element(test)[attr3='4']</match></xsl:template>

Output is:

<match>test</match>
<match>test</match>
<match>test</match>
<match>test</match>
<match>test</match>
<match>test</match>
fscore
  • 2,567
  • 7
  • 40
  • 74
  • Voted to close as typo: you're missing the `@` in your match expressions, you need `[@attr1='1']` instead of `[attr1='1']` (which would look for a child _element_ named `attr1` instead of an attribute). – Ian Roberts Oct 06 '14 at 17:45
  • 1
    well I am new to XSLT so did not know if that was a typo or not. – fscore Oct 06 '14 at 17:49

1 Answers1

3

The following updated XSLT will work as you expect:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="doc">
    <out>
        <xsl:apply-templates select="*"/>
    </out>
  </xsl:template>

  <xsl:template match="test"><match>test</match></xsl:template>
  <xsl:template match="test[@attr1='1']"><match>test[@attr1='1']</match></xsl:template>
  <xsl:template match="test[@attr1='2']"><match>test[@attr1='1']</match></xsl:template>
  <xsl:template match="test[@attr3='4']"><match>test[@attr1='1']</match></xsl:template>

</xsl:stylesheet>

Notes:

  1. element(test) can be simplified to just test in the match patterns.
  2. Use @ ahead of attribute names in the predicate patterns.
kjhughes
  • 106,133
  • 27
  • 181
  • 240