1

我有如下所示的代码,它给出了样式表编译错误。

<xsl:template match="form">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:variable name="param" select="name(.)" />
                <xsl:choose>
                    <xsl:when test="$param = 'name'">
                        <xsl:attribute name="name"><xsl:value-of
                            select="@name" /></xsl:attribute>
                    </xsl:when>
                    <xsl:when test="$param = 'action'">
                        <xsl:attribute name="action"><xsl:value-of
                            select="java:com.hp.cpp.proxy.util.URLUtils.rewriteAction($response, $baseurl, @action, $scope)" /></xsl:attribute>
                    </xsl:when>
                    <xsl:when test="$param = 'method'">
                        <xsl:attribute name="method">POST</xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:attribute name="$param"><xsl:value-of
                            select="." /></xsl:attribute>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
            <input type="hidden" name="httpmethod">
                <xsl:attribute name="value"> <xsl:value-of
                    select="@method" /></xsl:attribute>
            </input>
            <xsl:apply-templates select="node()|@*" />

        </xsl:copy>
    </xsl:template>

我正在尝试用相当复杂的要求重写 HTML 的 FORM 标记。希望您能够通过代码快照识别。我试图只重写标签的少数属性,并试图保留其余的。这是正确的方法吗?还有其他方法吗?任何建议。

提前致谢。

-里金

4

2 回答 2

0

关于编译错误,您没有提供足够的信息来提供帮助;Abel 关于编译错误与您对扩展函数的调用有关的猜想是合理的。

您还问这是正确的方法吗?实现你的目标。也许。您的第一个问题是 jvverde 已经指出的逻辑错误。应用模板的调用不应选择属性;您已经处理了所有属性。所以没有必要再次处理它们。这也是一个坏主意:如果您尝试再次处理form元素的属性,则会收到运行时错误,因为您已经将内容写入元素(即该input元素)。

我认为一些 XSLT 程序员会写一些看起来更像这样的东西:

<xsl:template match="form">
  <xsl:copy>

    <!--* don't use a for-each to handle the
        * attributes; use templates. *-->
    <xsl:apply-templates select="@*"/>

    <!--* you don't need an xsl:attribute constructor
        * if you want to use an expression within a 
        * literal result element; just braces in the
        * attribute-value template.
        *-->
    <input type="hidden" 
           name="httpmethod"
           value="{@method}" />

    <!--* change your apply-templates call to
        * select children, but not attributes.
        *-->
    <xsl:apply-templates select="node()" />
  </xsl:copy>
</xsl:template>

<!--* now the attributes ... *-->
<xsl:template match="form/@action">
  <xsl:attribute name="action">
    <xsl:value-of select="java:com.hp.cpp.proxy.util.URLUtils.rewriteAction(
                          $response, $baseurl, @action, $scope)" />
  </xsl:attribute>
</xsl:template>
<xsl:template match="form/@method">
  <xsl:attribute name="method">
    <xsl:value-of select="'POST'"/>
  </xsl:attribute>
</xsl:template>
于 2013-08-20T23:17:50.767 回答
0

只是一个猜测。尝试替换最后一个应用

<xsl:apply-templates select="node()|@*" />

这样

<xsl:apply-templates select="node()" />
于 2013-08-20T18:38:10.033 回答