在 Xalan XSLT 1.0 中,如何将变量传递给模板匹配? [英] In Xalan XSLT 1.0, how to pass a variable to a template match?

查看:27
本文介绍了在 Xalan XSLT 1.0 中,如何将变量传递给模板匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Java 中使用 Xalan XSLT 1.0,我们希望将变量传递给模板匹配以避免在 XSL 文件中硬编码元素名称.样式表编译成功,但返回的日期错误.我们使用的语法是否正确?

We are using Xalan XSLT 1.0 in Java and we want to pass a variable to a template match to avoid hard-coding element names in the XSL file. The style sheet compiles, but the date returned is wrong. Are we using the correct syntax?

可能的 XML 输入...

Possible XML inputs...

 <books>   
    <book/>
    <book/>
 </books>

 <dvds>
     <dvd/>
     <dvd/>
 </dvds>


<xsl:variable name="matchElement" select="'book'"/>
<!-- OR -->
<xsl:variable name="matchElement" select="'dvd'"/>

<xsl:template match="/*[local-name() = $matchElement]">  

推荐答案

This xsl:template:

<xsl:template match="/*[local-name() = $matchElement]"> 

从根匹配.

/* 中删除 / 或将其更改为 //*(取决于样式表的其余部分是如何设计的).

Either remove the / from /* or change it to //* (depending on how the rest of your stylesheet is designed).

此外,如果您使用 xsl:param 而不是 xsl:variable,则可以从命令行设置值.

Also, if you use xsl:param instead of xsl:variable, you can set the value from the command line.

这篇关于在 Xalan XSLT 1.0 中,如何将变量传递给模板匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆