【发布时间】:2019-02-09 11:51:49
【问题描述】:
我的源代码低于 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>
【问题讨论】:
标签: xml xslt namespaces