【问题标题】:Why is there xmlns in my html output为什么我的 html 输出中有 xmlns
【发布时间】:2013-11-06 09:57:24
【问题描述】:

在来自 XSLT 进程(使用 saxon9he)的 html 输出文件中,出现了 155 次 xmlns:fn="http://www.w3.org/2005/xpath-functions" 插入各种tr元素

xsl 中使用 xpath-functions 的部分是

<xsl:if test="(string(@hideIfHardwareIs)='') or (not(fn:matches(string($input_doc//inf[@id='5'), string(@hideIfHardwareIs), 'i')))">

除非我读错了,matches 需要 3 个参数,一个字符串,另一个字符串,然后是一个标志,在这种情况下这是不区分大小写的。

我不明白的是,与 xmlns 一起显示的 tr 元素并不靠近执行 match() 函数的部分或 xsl。

我正在使用的 XSL 文件是 2100 行,它解析的 XML 文件是 12800 行。所以我认为我不能轻易分享它。我已经继承了它,需要(此时)维护它。

我可以在 XSL 中寻找哪些可以将 xmlns 插入到 html 输出中的东西?

【问题讨论】:

  • 哦,我可能已经想通了。在 xsl 之外创建的任何元素似乎都将这个 xmlns 插入其中。我正在检查,如果这是真的,我会回复。当然,除非其他人知道这就是发生这种情况的原因。

标签: html xml xslt xpath


【解决方案1】:

这些函数不需要前缀。

xsl:stylesheet 中删除 xmlns:fn="http://www.w3.org/2005/xpath-functions" 并从 xpath 函数中删除 fn: 前缀。

例子:

XML 输入

<foo>test</foo>

XSLT 2.0 #1

<xsl:stylesheet version="2.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:if test="fn:matches(.,'^t')">
            <bar><xsl:value-of select="."/></bar>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

输出

<bar xmlns:fn="http://www.w3.org/2005/xpath-functions">test</bar>

XSLT 2.0 #2

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:if test="matches(.,'^t')">
            <bar><xsl:value-of select="."/></bar>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

输出

<bar>test</bar>

【讨论】:

  • 正确答案。某些 IDE 会在每个样式表的开头插入此命名空间声明。这是完全没有必要的。如果您确实想包含它(因为出于某种原因您更喜欢编写 fn:position() 而不是 position()),您应该将它包含在 exclude-result-prefixes 属性中,这样它就不会进入输出。
猜你喜欢
  • 2021-11-27
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多