Domanda

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?
È stato utile?

Soluzione

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.

Altri suggerimenti

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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top