Question

When applyng this XSLT:

<xsl:template match="e">
  <xsl:value-of select="@name"/>
</xsl:template>

To this xml:

   <root>
     <e name="1"/>
     <la>
      <e name="bla"/>
     </la> 
   </root>

I get both "1" and "bla".

  1. Why is this so?
  2. How can I make sure that the XSLT is applied only to the direct children of root?
Était-ce utile?

La solution

Did you try match="root/e"? If you want to match nodes in a certain context, you need to provide the context in the rule, otherwise all nodes with the matching node name apply to the rule.

Autres conseils

You may also use something like:

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

  <xsl:template match="root">
    <xsl:apply-templates select="child::e"/>
  </xsl:template>

  <xsl:template match="e">
    <xsl:value-of select="@name"/>
  </xsl:template>

</xsl:stylesheet>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top