[英]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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.