我认为这很简单,但现在我在这个问题上花了大约 4 个小时。我想要做的就是更改此 XML 的默认命名空间:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<scenarios xmlns="http://my.url/xmlns/scenarios/v1.0.0">
<scenarios>
<scenario id="1" name="00_reset" active="true">
<events>
<sensorevent id="1" name="resetButtonEvent">
<sensors>
<sensor deviceid="46"/>
</sensors>
</sensorevent>
</events>
</scenario>
</scenarios>
<systemstates>
<systemstate id="1" default="true" name="00_visitor_reset" display="true" publish="true" type="BOOLEAN"/>
</systemstates>
</scenarios>
“v1.0.0”应更改为“v1.1.0”。但我能想到的是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:old="http://my.url/xmlns/scenarios/v1.0.0"
xmlns:new="http://my.url/xmlns/scenarios/v1.1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="old:*">
<xsl:element name="{local-name()}" namespace="http://my.url/xmlns/scenarios/v1.1.0">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
它产生:
<?xml version="1.0" encoding="UTF-8"?><ns0:scenarios xmlns:ns0="http://my.url/xmlns/scenarios/v1.1.0">
<ns1:scenarios xmlns:ns1="http://my.url/xmlns/scenarios/v1.1.0">
<ns2:scenario xmlns:ns2="http://my.url/xmlns/scenarios/v1.1.0" id="1" name="00_reset" active="true">
<ns3:events xmlns:ns3="http://my.url/xmlns/scenarios/v1.1.0">
<ns4:sensorevent xmlns:ns4="http://my.url/xmlns/scenarios/v1.1.0" id="1" name="resetButtonEvent">
<ns5:sensors xmlns:ns5="http://my.url/xmlns/scenarios/v1.1.0">
<ns6:sensor xmlns:ns6="http://my.url/xmlns/scenarios/v1.1.0" deviceid="46"/>
</ns5:sensors>
</ns4:sensorevent>
</ns3:events>
</ns2:scenario>
</ns1:scenarios>
<ns7:systemstates xmlns:ns7="http://my.url/xmlns/scenarios/v1.1.0">
<ns8:systemstate xmlns:ns8="http://my.url/xmlns/scenarios/v1.1.0" id="1" default="true" name="00_visitor_reset" display="true" publish="true" type="BOOLEAN"/>
</ns7:systemstates>
</ns0:scenarios>
这并不是我真正想要的,因为唯一应该改变的是根元素中的命名空间声明。如何从 http://my.url/xmlns/scenarios/v1.0.0 更改默认命名空间至 http://my.url/xmlns/scenarios/v1.1.0使用 XSLT1.0 而不添加前缀?
最佳答案
尝试
<xsl:stylesheet version="1.0" xmlns:old="http://my.url/xmlns/scenarios/v1.0.0"
xmlns="http://my.url/xmlns/scenarios/v1.1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="old:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
如果这没有帮助,请尝试不同的 XSLT 1.0 处理器。
关于xml - 使用 XSL 替换默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904623/