質問

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?
役に立ちましたか?

解決

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.

他のヒント

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top