删除 XML 中的子节点并使用 XSLT 将它们的数据复制到父节点
Removing Child nodes in XML and copying their data to parent node using XSLT
我试图消除所有子节点并将所有数据复制到父节点,但输出与输入保持相同。
输入XML -
<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>
<Header>
</Header>
<Person>
<Personal>
<FirstName>abc</FirstName>
<LastName>cde</LastName>
<ID>12345</ID>
</Personal>
<AddressData>
<Address1>abc123</Address1>
<Address2>def345</Address2>
</AddressData>
<PhoneData>
<Phone1>111111111</Phone1>
</PhoneData>
</Person>
</PersonData>
我已经尝试了下面的代码,但输出与输入保持相同,因此不会删除子节点,并且保留在其中的数据不会移动到父节点,即 Person。
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Person">
<xsl:copy>
<xsl:apply-templates select=""@*|node()[not(self::Person)]""/>
</xsl:copy>
<xsl:apply-templates select="Person" />
</xsl:template>
</xsl:stylesheet>
期望的输出 -
<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>
<Person>
<FirstName>abc</FirstName>
<LastName>cde</LastName>
<ID>12345</ID>
<Address1>abc123</Address1>
<Address2>def345</Address2>
<Phone1>111111111</Phone1>
</Person>
</PersonData>
我得到与输入相同的输出 XML 而不是上面没有子节点的预期输出
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/PersonData">
<xsl:copy>
<xsl:apply-templates select="Person"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Person">
<xsl:copy>
<xsl:copy-of select="*/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我试图消除所有子节点并将所有数据复制到父节点,但输出与输入保持相同。
输入XML -
<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>
<Header>
</Header>
<Person>
<Personal>
<FirstName>abc</FirstName>
<LastName>cde</LastName>
<ID>12345</ID>
</Personal>
<AddressData>
<Address1>abc123</Address1>
<Address2>def345</Address2>
</AddressData>
<PhoneData>
<Phone1>111111111</Phone1>
</PhoneData>
</Person>
</PersonData>
我已经尝试了下面的代码,但输出与输入保持相同,因此不会删除子节点,并且保留在其中的数据不会移动到父节点,即 Person。
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Person">
<xsl:copy>
<xsl:apply-templates select=""@*|node()[not(self::Person)]""/>
</xsl:copy>
<xsl:apply-templates select="Person" />
</xsl:template>
</xsl:stylesheet>
期望的输出 -
<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonData>
<Person>
<FirstName>abc</FirstName>
<LastName>cde</LastName>
<ID>12345</ID>
<Address1>abc123</Address1>
<Address2>def345</Address2>
<Phone1>111111111</Phone1>
</Person>
</PersonData>
我得到与输入相同的输出 XML 而不是上面没有子节点的预期输出
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/PersonData">
<xsl:copy>
<xsl:apply-templates select="Person"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Person">
<xsl:copy>
<xsl:copy-of select="*/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>