在项目中声明全局变量并在 xslt 中使用它 [英] declare global variable in project and use it in xslt

查看:38
本文介绍了在项目中声明全局变量并在 xslt 中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会寻求您对 XSLT 中的疑问的指导.在我当前的项目中,需要创建许多 XSLT 文件.在这些转换中,执行的常见步骤很少;例如从输入 xml 更改元素值的大写.我目前在 XSLT 中使用以下代码,因此如果创建了 50 个 XSLT,则此代码将被复制.

I would seek this your guidance for doubt in XSLT. In my current project there is a requirement to create many XSLT files. In these transformations, there are few common steps performed; for.eg. changing uppercase of an element value from input xml. I'm currently using the below code in an XSLT, so if there are 50 XSLT being created then this code will be duplicated.

<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
            <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 
            <xsl:message>UPPERCASE is <xsl:value-of select="translate($MsgType, $smallcase, $uppercase)" /></xsl:message>  

就如何避免代码重复征求您的建议.我可以创建一个通用的 XML 文件(例如实用程序)并声明变量大写和小写,我应该在 xslt.xml 文件中调用这些变量吗?类似于其他编.lang like java,我可以在其中全局声明一个通用函数并在不同的类中使用它.基本上我想知道是否可以全局声明并在所有xslt中使用它.请提供建议,因为我是 XSLT 编程的新手.谢谢.

Requesting your advice on how to avoid code duplication. Can I create a common XML file such as utility and declare the variables uppercase and smallcase and shall I call these variables inside the xslt. Similar to other prog. lang like java where I can declare a common function globally and use it in different classes. Basically I would like to know whether it is possible to declare globally and use it in all the xslt. Pls advice since I'm new to XSLT programming. Thanks.

推荐答案

我会使用 <include/> 来包含定义了所有全局变量的 XSLT 文件.另见 http://www.w3.org/TR/xslt#element-include

I would use <include/> to include the XSLT file with all your global variables defined. See also http://www.w3.org/TR/xslt#element-include

将所有变量放入文件my_global_variables.xsl":

Put all your variables into the file "my_global_variables.xsl":

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="myVariable" select="'xyz'"/>

<!-- more variables to add -->

</xsl:stylesheet>

您的主样式表如下所示,包括my_global_variables.xsl":

Your main stylesheet looks like this then, including the "my_global_variables.xsl":

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:include href="my_global_variables.xsl"/>

<xsl:template match="/">

</xsl:template>
</xsl:stylesheet>

希望对你有帮助.

还有 元素,您可以使用它导入样式表.不过,导入的样式表的优先级低于导入的样式表 - 所以在你的情况下,我会使用 .

There is also the <import> element with which you can import stylesheets. An imported style sheet has lower precedence than the importing style sheet though - so in your case I would use <include>.

最好的问候,彼得

这篇关于在项目中声明全局变量并在 xslt 中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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