我们如何根据另一个 xml 文档过滤一个 xml 文档。我必须删除查找 xml 中不存在的所有元素。输入 xml 和查找 xml 具有相同的根元素,我们使用 XSLT 1.0。
Ex 输入
<Root>
<E1 a="1">V1</E1>
<E2>V2</E2>
<E3>V3</E3>
<E5>
<SE51>SEV1</SE51>
<SE52>SEV2</SE52>
</E5>
<E6>
<SE61>SEV3</SE61>
<SE62>SEV4</SE62>
</E6>
</Root>
过滤 XML
<Root>
<E1 a="1"></E1>
<E2></E2>
<E5>
<SE51></SE51>
<SE52></SE52>
</E5>
</Root>
预期输出
<Root>
<E1 a="1">V1</E1>
<E2>V2</E2>
<E5>
<SE51>SEv1</SE51>
<SE52>SEV2</SE52>
</E5>
</Root>
最佳答案
这是所需的转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:z="inline:text.xml" exclude-result-prefixes="z" > <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <z:filter> <Root> <E1 a="1"></E1> <E2></E2> <E5> <SE51></SE51> <SE52></SE52> </E5> </Root> </z:filter> <xsl:variable name="vFilter" select= "document('')/*/z:filter"/> <xsl:template match="/"> <xsl:apply-templates select="*[name()=name($vFilter/*)]"> <xsl:with-param name="pFiltNode" select="$vFilter/*"/> </xsl:apply-templates> </xsl:template> <xsl:template match="*"> <xsl:param name="pFiltNode"/> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:for-each select="text() | *"> <xsl:choose> <xsl:when test="self::text()"> <xsl:copy-of select="."/> </xsl:when> <xsl:otherwise> <xsl:variable name="vFiltNode" select="$pFiltNode/*[name()=name(current())]"/> <xsl:apply-templates select="self::node()[$vFiltNode]"> <xsl:with-param name="pFiltNode" select="$vFiltNode"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet>
当此转换应用于以下 XML 文档时(原始文档加上 <SE511>SEV11</SE511>
以证明过滤在任何级别都有效:
<Root> <E1 a="1">V1</E1> <E2>V2</E2> <E3>V3</E3> <E5> <SE51>SEV1</SE51> <SE511>SEV11</SE511> <SE52>SEV2</SE52> </E5> <E6> <SE61>SEV3</SE61> <SE62>SEV4</SE62> </E6> </Root>
产生了想要的结果:
<Root> <E1 a="1">V1</E1> <E2>V2</E2> <E3>V3</E3> <E5> <SE51>SEV1</SE51> <SE511>SEV11</SE511> <SE52>SEV2</SE52> </E5> <E6> <SE61>SEV3</SE61> <SE62>SEV4</SE62> </E6> </Root>
请注意此解决方案的以下详细信息:
- 模板仅应用于在过滤文档中具有匹配节点的元素以及此类元素的所有文本节点。
- 与元素匹配的模板作为参数传递到过滤器文档中的相应节点。
- 将模板应用于子元素时,会找到其对应的节点并将其作为预期参数传递。
尽情享受吧!
关于xml - 使用 XSLT 1 根据另一个 xml 过滤 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/336572/