【问题标题】:Select the string of an XSLT param given at command line选择在命令行中给出的 XSLT 参数的字符串
【发布时间】:2012-10-01 17:11:57
【问题描述】:

我正在使用 XSLT 参数在运行时使用 Xalan-C 在属性中设置绝对路径。基本上,我的输入 XML 是这样的:-

<root xmlns="initial">
  <!-- document goes here -->
</root>

我的样式表是:-

<xsl:stylesheet version="1.0" xmlns:s="initial" xmlns="final" />

  <xsl:param name="default_data_location">/path/to/some/location</xsl:param>

  <xsl:template match="//s:*">
    <xsl:element name="{local-name()}" namespace="final">
      <xsl:attribute name="dataLocation">
        <xsl:value-of select="concat($default_data_location, '/datafile')"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- rest of the stylesheet -->

</xsl:stylesheet>

因此,当我运行它时,我想要的输出 XML 为:-

Xalan foo.xml foo.xsl 

应该是(这是有效的部分):-

<root xmlns="final" dataLocation="/path/to/some/location/datafile">
  <!-- document goes here -->
</root>

当我运行它时:-

Xalan -p default_data_location /some/other/path foo.xml foo.xsl

应该是(这是不起作用的部分):-

<root xmlns="final" dataLocation="/some/other/path/datafile">
  <!-- document goes here -->
</root>

但是,如果我尝试在命令行中设置此参数,它会给我以下 XML:-

<root xmlns="final" dataLocation="/datafile">
  <!-- document goes here -->
</root>

我该怎么办?

【问题讨论】:

    标签: string xslt xpath parameters xalan


    【解决方案1】:

    参数值似乎是一个 XPath 表达式,因此您需要确保传入一个 XPath 字符串,并且您可能需要双引号以确保命令行 shell 不会进入您的方式,因此 Xalan -p default_data_location "'/some/other/path'" foo.xml foo.xsl 应该工作。至少这是我在http://xml.apache.org/xalan-c/commandline.html 阅读的文档,我没有 Xalan-C 来测试。

    【讨论】:

    • nopes 没用。我已经尝试了所有可能的包含在“”和“”中的组合。实际上,这可能不是因为 xpath 字符串。即使我传入一些垃圾说 Xalan -p default_data_location 'sdndsa' 不起作用,它也根本不输出任何值。
    • 我认为 shell 可能会妨碍您,因此我会在 Windows 上尝试诸如 Xalan -p default_data_location "'/some/other/path'" foo.xml foo.xsl 之类的东西。不过今晚我没有时间测试,希望其他 Xalan-C 用户可以提供帮助。
    • 就是这样。我不确定我在绝望的蛮力搜索中是如何错过的。更新您的答案,我会将其标记为已接受。
    猜你喜欢
    • 2021-08-19
    • 2017-03-19
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多