【问题标题】:Create global variable in XSL from specific tag in XML从 XML 中的特定标记在 XSL 中创建全局变量
【发布时间】:2012-11-30 06:47:47
【问题描述】:

我在 XSL 1.0 样式表中创建全局变量时遇到问题。我想从我试图转换的 XML 中的 XML 标记的值创建变量。这是我的 XML 的样子:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<config name="test report" xmlns="http://www.example.com/CONFIG">

    <the_one_i_want>1000</the_one_i_want>

    <!-- lots of other stuff -->

</config>

这是我的 XSL 的样子:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:CONFIG="http://www.example.com/CONFIG">

    <xsl:output method="html"/>

    <xsl:variable name="normal_global_variable">100</xsl:variable><!-- This works fine -->
    <xsl:variable name="variable_from_xml"><xsl:value-of select="/config/the_one_i_want/value"/></xsl:variable><!-- This does not work -->

    <!-- lots of other stuff -->

</xsl:stylesheet>

所以我希望variable_from_xml 的值为1000,但事实并非如此。我做错了什么?

P.S.名为 the_one_i_want 的 XML 标记是唯一的,在我的 XML 中只出现一次。

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    问题是命名空间之一。您所追求的&lt;the_one_i_want&gt; 元素绑定到http://www.example.com/CONFIG 命名空间(您已经在XSLT 中定义了它)。

    因此,只需更改以下内容:

    <xsl:variable name="variable_from_xml">
      <xsl:value-of select="/config/the_one_i_want/value"/>
    </xsl:variable>
    

    对此:

    <xsl:variable name="variable_from_xml" select="/CONFIG:config/CONFIG:the_one_i_want"/>
    

    或者,更简单地说:

    <xsl:variable name="variable_from_xml" select="/*/CONFIG:the_one_i_want"/>
    

    【讨论】:

    • 没有尝试第一个解决方案,但第二个解决方案效果很好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2021-12-11
    • 2011-01-02
    相关资源
    最近更新 更多