我定义了以下 XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="ns0.com" xsi:schemaLocation="ns0.com ns0.xsd">
<ns1:elementA xmlns:ns1="ns1.com" xsi:schemaLocation="ns1.com ns1.xsd"/>
<ns2:elementB xmlns:ns2="ns2.com" xsi:schemaLocation="ns2.com ns2.xsd"/>
</ns0:container>
问题是消费应用程序只取容器内部的元素(不幸的是容器的字符串切割),然后缺少命名空间xsi的定义。
我想添加
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
到容器的每个子元素 - 这将是一个冗余的规范,但不应引起任何问题。所以这是我想得到的结果:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="container.com" xsi:schemaLocation="ns0.com ns0.xsd">
<ns1:elementA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="ns1.com" xsi:schemaLocation="ns1.com ns1.xsd" />
<ns2:elementB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="ns2.com" xsi:schemaLocation="ns2.com ns2.xsd"/>
</ns0:container>
这是我的 XSLT,我尝试了几个选项,但无法做到:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="ns0.com"
xmlns:ns1="ns1.com"
xmlns:ns2="ns2.com">
<xsl:output method="xml" indent="no"/>
<xsl:template match="ns0:container/*">
<xsl:copy>
<!-- Here I want to add the xmlns:xsi as attribute -->
<xsl:attribute name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
<!-- But this does not work - how should I do that? -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如何使用 XSLT 向元素添加额外的 xmlns:xsi=""?
最佳答案
尝试这样的事情
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="ns0.com">
<xsl:output method="xml" indent="no"/>
<xsl:template match="ns0:container/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/*/@xsi:*">
<xsl:attribute name="other:{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
编辑:
你甚至可以把它简化成这样
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/*/@xsi:*">
<xsl:attribute name="other:{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我认为在您的 xml 中,您在声明 ns0 的命名空间 (xmlns:ns0="container.com") 中也有拼写错误。我想你的意思是 (xmlns:ns0="ns0.com")
关于xml - 添加额外的 xmlns :xsi attributes to XML element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329341/