XSLT - 将相似的节点组合在一起并删除重复的节点
XSLT - Group similar nodes together and remove duplicate nodes
我有以下XML
<?xml version="1.0"?>
<R>
<M>
<H>1</H>
<B>1</B>
</M>
<M>
<H>1</H>
<B>2</B>
</M>
<M>
<H>1</H>
<B>3</B>
</M>
<M>
<H>1</H>
<B>4</B>
</M>
</R>
这里假设'M'是主节点,'H'是header,'B'是body,我想只有一个'M'节点'H' 及其下的所有 'B' 个节点。
基本上我想将所有 'B' 节点移动到第一个 'M' 节点并删除所有其他 'M' 和 'H' 标签。
任何人都可以帮助我实现这一点。
预期输出为:
<?xml version="1.0"?>
<R>
<M>
<H>1</H>
<B>1</B>
<B>2</B>
<B>3</B>
<B>4</B>
</M>
</R>
这是我当前的 XSLT 脚本:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<R>
<xsl:apply-templates select="@*|M/*" />
<xsl:apply-templates select="@*|M/B" />
</R>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
简单的描述"Basically I want to move all the 'B' nodes to first 'M' node and remove all other 'M' and 'H' tags"似乎很容易被
解决
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="R">
<xsl:copy>
<M>
<xsl:apply-templates select="M[1]/H | M/B"/>
</M>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ej9EGbG
如果 H
元素有不同的值,不确定是否需要任何分组。
我有以下XML
<?xml version="1.0"?>
<R>
<M>
<H>1</H>
<B>1</B>
</M>
<M>
<H>1</H>
<B>2</B>
</M>
<M>
<H>1</H>
<B>3</B>
</M>
<M>
<H>1</H>
<B>4</B>
</M>
</R>
这里假设'M'是主节点,'H'是header,'B'是body,我想只有一个'M'节点'H' 及其下的所有 'B' 个节点。
基本上我想将所有 'B' 节点移动到第一个 'M' 节点并删除所有其他 'M' 和 'H' 标签。
任何人都可以帮助我实现这一点。
预期输出为:
<?xml version="1.0"?>
<R>
<M>
<H>1</H>
<B>1</B>
<B>2</B>
<B>3</B>
<B>4</B>
</M>
</R>
这是我当前的 XSLT 脚本:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<R>
<xsl:apply-templates select="@*|M/*" />
<xsl:apply-templates select="@*|M/B" />
</R>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
简单的描述"Basically I want to move all the 'B' nodes to first 'M' node and remove all other 'M' and 'H' tags"似乎很容易被
解决<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="R">
<xsl:copy>
<M>
<xsl:apply-templates select="M[1]/H | M/B"/>
</M>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ej9EGbG
如果 H
元素有不同的值,不确定是否需要任何分组。