XSL的异常模板行为

时间:2010-12-30 01:27:42

标签: xslt

经历了一些非常奇怪的行为,应该是,XSL和XSLT的一个非常简单的使用。

这是一个代码示例。

<xsl:template match="check">

<div class="check">

<xsl:apply-templates mode="check">
<xsl:with-param name="checkName">testVariable</xsl:with-param>
</xsl:apple-templates>

</div>

</xsl:template>

上面提到的模板

<xsl:template match="option" mode="check">
    <xsl:param name="checkName" />

    <div class="option">
        <input type="checkbox">
        </input>

        <label>
            testText
        </label>
    </div>
</xsl:template>

非常简单吧? 它应该为XML中的每个实例实例创建一个带有硬编码标签的复选框。

但是,我得到的是

<div class="check"></div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>
<div class="check"></div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>
<div class="option>Checkbox stuff here</div>

以下是一些示例XML

<check><option key="1"/><option key="0"/><option
            key="0"/><option key="0"/><option
        key="0"/></check>

任何人都知道发生了什么事吗? :d

1 个答案:

答案 0 :(得分:2)

问题出在您未向我们展示的XML文档/代码中,或者您使用有缺陷的XSLT处理器的可能性较小。

此转化(您的代码包含在<xsl:stylesheet>中):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="check">
   <div class="check">
    <xsl:apply-templates mode="check">
     <xsl:with-param name="checkName">testVariable</xsl:with-param>
    </xsl:apply-templates>
   </div>
 </xsl:template>

  <xsl:template match="option" mode="check">
    <xsl:param name="checkName" />
    <div class="option">
      <input type="checkbox"></input>
        <label>testText</label>
    </div>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<check>
    <option key="1"/>
    <option key="0"/>
    <option key="0"/>
    <option key="0"/>
    <option key="0"/>
</check>

生成想要的正确结果

<div class="check">
    <div class="option">
        <input type="checkbox"></input>
        <label>testText</label>
    </div>
    <div class="option">
        <input type="checkbox"></input>
        <label>testText</label>
    </div>
    <div class="option">
        <input type="checkbox"></input>
        <label>testText</label>
    </div>
    <div class="option">
        <input type="checkbox"></input>
        <label>testText</label>
    </div>
    <div class="option">
        <input type="checkbox"></input>
        <label>testText</label>
    </div>
</div>

我已经确认,使用

运行时,转换会产生相同的结果
  • Saxon 6.5.4

  • MSXML3,MSXML4,MSXML6

  • .NET XslCompiledTransform和XslTransform

  • Saxon 9.1.05

  • AltovaXML(XML-SPY)

  • XQSharp