我正在使用XML作为源,必须通过添加段进行转换。但是必须删除根节点中添加的名称空间。但是无法删除名称空间。
有人可以告诉我添加到XSLT的位置吗?
源XML:
<?xml version="1.0" encoding="UTF-8"?>
<Header
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<Main>
<Parent2>
<status>12</status>
<statusmsg>Helo</statusmsg>
</Parent2>
<Parent3>
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4>
<Child3>Valley</Child3>
<Parent5>
<Child7>Kind</Child7>
<Child8>Pls</Child8>
</Parent5>
</Parent4>
</Main>
</Header>
目标XML:
<Header>
<Main Mainattribute="1">
<Parent2 childattribute="1">
<status>12</status>
<statusmsg>Helo</statusmsg>
</Parent2>
<Parent3 childattribute="1">
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4 childattribute="1">
<Child3>Valley</Child3>
<Parent5>
<Child7>Kind</Child7>
<Child8>Pls</Child8>
</Parent5>
</Parent4>
</Main>
</Header>
XSLT从下面的链接尝试过: Populate Attribute and values to all parent nodes of the XML file from 4th parent node
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates mode="parent_mode"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="parent_mode">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
未使用的名称空间声明不应真正引起任何问题。但是,如果您确实想摆脱它,那么在XSLT 1.0中,您将不得不使用xsl:element
创建一个新元素,而不是使用xsl:copy
,因为后者会将名称空间声明复制到其中。>
尝试使用此XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main">
<Main>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates mode="parent_mode"/>
</Main>
</xsl:template>
<xsl:template match="*" mode="parent_mode">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
如果可以使用XSLT 2.0,则可以将copy-namespaces
添加到xsl:copy
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates mode="parent_mode"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="parent_mode">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
对于XSLT 2.0,有一种更简单的方法是使用
<xsl:copy-of select="/" copy-namespaces="no"/>
进行深度复制,删除所有未使用的名称空间。