문제

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