Safari XSLT 引擎在属性上丢失命名空间
Safari XSLT engine loses namespace on attributes
我有一个匹配特定属性的 XSLT,并将它们放在不同的名称空间中。这是一个简化版本:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:test:ns1"
xmlns:ns2="urn:test:ns2">
<xsl:output method="xml" indent="no" encoding="UTF-8"/>
<!-- copy all nodes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[starts-with(local-name(), 'test-')]">
<xsl:attribute name="ns2:{substring-after(local-name(), '-')}" namespace="urn:test:ns2">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
这是一些示例输入:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns3="urn:test:ns3"
rootAttr="stays in implicit namespace"
ns3:passMe="stays in the ns3 namespace"
test-someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
test-someAttr="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
test-catName="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
这是预期的输出:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns2="urn:test:ns2"
xmlns:ns3="urn:test:ns3"
rootAttr="stays in implicit namespace"
ns3:passMe="stays in the ns3 namespace"
ns2:someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
ns2:someAttr="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
ns2:catName="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
这在 Chrome、Firefox、IE 9+ 和 Android 上运行良好。但是在 Safari 上,我得到以下输出:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns3="urn:test:ns3"
xmlns:ns2="urn:test:ns2"
rootAttr="stays in implicit namespace"
passMe="stays in the ns3 namespace"
someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
someAttr="goes into the ns2 namespace"
namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
catName="goes into the ns2 namespace"
namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
请注意命名空间 声明 是正确的,但属性缺少所需的命名空间前缀。
所有这些代码都在 github project, which is built by TravisCI and uses Sauce Labs 中以测试不同的 browser/OS 组合。
我能否对我的 XSLT 做一些不同的事情,这将是完成此任务的更正确的方法,并且可能适用于所有引擎?或者这只是 Safari 中的一个错误?任何解决方法的想法将不胜感激。
我认为这是一个错误。作为解决方法,您可以尝试在 xsl:attribute namespace="urn:test:ns2"
.
上设置您想要的命名空间
在没有任何相反证据的情况下,这似乎是 Safari 中的错误。我已将此事报告给 Apple (rdar://23207226
),但到目前为止还没有收到他们的任何消息。
唯一可用的解决方法是 运行 某些其他引擎(服务器端,或者可能通过第 3 方 javascript 引擎)上的 XSLT。
我有一个匹配特定属性的 XSLT,并将它们放在不同的名称空间中。这是一个简化版本:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:test:ns1"
xmlns:ns2="urn:test:ns2">
<xsl:output method="xml" indent="no" encoding="UTF-8"/>
<!-- copy all nodes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[starts-with(local-name(), 'test-')]">
<xsl:attribute name="ns2:{substring-after(local-name(), '-')}" namespace="urn:test:ns2">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
这是一些示例输入:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns3="urn:test:ns3"
rootAttr="stays in implicit namespace"
ns3:passMe="stays in the ns3 namespace"
test-someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
test-someAttr="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
test-catName="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
这是预期的输出:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns2="urn:test:ns2"
xmlns:ns3="urn:test:ns3"
rootAttr="stays in implicit namespace"
ns3:passMe="stays in the ns3 namespace"
ns2:someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
ns2:someAttr="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
ns2:catName="goes into the ns2 namespace"
ns3:namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
这在 Chrome、Firefox、IE 9+ 和 Android 上运行良好。但是在 Safari 上,我得到以下输出:
<?xml version="1.0" encoding="UTF-8" ?>
<hello-world
xmlns="urn:test:ns1"
xmlns:ns3="urn:test:ns3"
xmlns:ns2="urn:test:ns2"
rootAttr="stays in implicit namespace"
passMe="stays in the ns3 namespace"
someRootAttr="goes into the ns2 namespace, pulls up ns declaration">
<test
defaultAttr="stays in implicit namespace"
someAttr="goes into the ns2 namespace"
namedAttr="stays in the ns3 namespace">
Something
</test>
<ns3:cat
defaultAttr="stays in the implicit namespace"
catName="goes into the ns2 namespace"
namedAttr="stays in the ns3 namespace">
a cat
</ns3:cat>
</hello-world>
请注意命名空间 声明 是正确的,但属性缺少所需的命名空间前缀。
所有这些代码都在 github project, which is built by TravisCI and uses Sauce Labs 中以测试不同的 browser/OS 组合。
我能否对我的 XSLT 做一些不同的事情,这将是完成此任务的更正确的方法,并且可能适用于所有引擎?或者这只是 Safari 中的一个错误?任何解决方法的想法将不胜感激。
我认为这是一个错误。作为解决方法,您可以尝试在 xsl:attribute namespace="urn:test:ns2"
.
在没有任何相反证据的情况下,这似乎是 Safari 中的错误。我已将此事报告给 Apple (rdar://23207226
),但到目前为止还没有收到他们的任何消息。
唯一可用的解决方法是 运行 某些其他引擎(服务器端,或者可能通过第 3 方 javascript 引擎)上的 XSLT。