我需要从输入 XML 中删除命名空间,并保留当前消除子节点的处理并将数据移动到 XSLT 中的父节点。 基于各种线程,我尝试通过定义另一个命名空间来删除命名空间,但我无法获得所需的输出。
下面用于消除子节点和移动数据的 XSLT 代码可以正常工作,但用于删除命名空间的代码则无法正常工作。
XSLT 代码 -
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bc="urn:com.matrix/bc"
xmlns:test="urn:com.matrix/test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="bc test">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="bc:PersonData">
<xsl:copy>
<xsl:apply-templates select="bc:Person"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bc:Person">
<xsl:copy>
<xsl:copy-of select="*/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输入 XML -
<?xml version="1.0" encoding="ISO-8859-1"?>
<bc:PersonData xmlns:bc="urn:com.matrix/bc">
<bc:Header>
<data1>abc</data1>
<data2>def</data2>
</bc:Header>
<bc:Person>
<bc:Personal>
<bc:FirstName>abc</bc:FirstName>
<bc:LastName>cde</bc:LastName>
<bc:ID>12345</bc:ID>
</bc:Personal>
<bc:Address>
<bc:Address1>abc123</bc:Address1>
<bc:Address2>def345</bc:Address2>
<bc:Address3>uyt57</bc:Address3>
</bc:Address>
<bc:PhoneData>
<bc:Phone1>111111111</bc:Phone1>
</bc:PhoneData>
</bc:Person>
</bc:PersonData>
预期输出 XML - 没有子节点和命名空间
<?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>
<Address3>uyt57</Address3>
<Phone1>111111111</Phone1>
</Person>
</PersonData>
最佳答案
这是一个更短(20 行)且更通用的解决方案。也尽可能使用推送式:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bc="urn:com.matrix/bc">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*[namespace-uri()]">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*[namespace-uri()]">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="bc:Person/*"><xsl:apply-templates/></xsl:template>
<xsl:template match="/*/*[not(self::bc:Person)]"/>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bc:PersonData xmlns:bc="urn:com.matrix/bc">
<bc:Header>
<data1>abc</data1>
<data2>def</data2>
</bc:Header>
<bc:Person>
<bc:Personal>
<bc:FirstName>abc</bc:FirstName>
<bc:LastName>cde</bc:LastName>
<bc:ID>12345</bc:ID>
</bc:Personal>
<bc:Address>
<bc:Address1>abc123</bc:Address1>
<bc:Address2>def345</bc:Address2>
<bc:Address3>uyt57</bc:Address3>
</bc:Address>
<bc:PhoneData>
<bc:Phone1>111111111</bc:Phone1>
</bc:PhoneData>
</bc:Person>
</bc:PersonData>
产生了想要的正确结果:
<PersonData>
<Person>
<FirstName>abc</FirstName>
<LastName>cde</LastName>
<ID>12345</ID>
<Address1>abc123</Address1>
<Address2>def345</Address2>
<Address3>uyt57</Address3>
<Phone1>111111111</Phone1>
</Person>
</PersonData>
关于xml - 在 XSLT 转换期间删除 XML 命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57084981/