xslt xml 第一次出现并合并标签 [英] xslt xml getting first occurrence and merging the tags
本文介绍了xslt xml 第一次出现并合并标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是 XML,我正在寻找如下输出
Below is the XML and I am looking for output as below
输入 XML (xml version="1.0")
<dynamic>
<rpc xmlns="http://namespace/example" >
<route>
<table>
<tablename>employee</tablename>
<count>20</count>
</table>
<table>
<tablename>employee</tablename>
<count> 21</count>
<rt> 1</rt>
<rt> 2</rt>
<rt> 3</rt>
<rt> 4</rt>
</table>
<table>
<tablename>dept</tablename>
<count>20</count>
<rt> a</rt>
<rt> b</rt>
<rt> c</rt>
</table>
<table>
<tablename>dept</tablename>
<count>44</count>
<rt> d</rt>
<rt> e</rt>
<rt> g</rt>
</table>
</route>
</rpc>
</dynamic>
请注意,只需要选择第一个
值
输出 XML
<dynamic>
<rpc xmlns="http://namespace/example">
<route>
<table>
<tablename>employee</tablename>
<count>20</count>
<rt> 1</rt>
<rt> 2</rt>
<rt> 3</rt>
<rt> 4</rt>
</table>
<table>
<tablename>dept</tablename>
<count>20</count>
<rt> a</rt>
<rt> b</rt>
<rt> c</rt>
<rt> d</rt>
<rt> e</rt>
<rt> g</rt>
</table>
</route>
</rpc>
</dynamic>
推荐答案
一个选项使用 慕尼黑分组...
编辑以处理默认命名空间.
XML
<dynamic>
<rpc xmlns="http://namespace/example" >
<route>
<table>
<tablename>employee</tablename>
<count>20</count>
</table>
<table>
<tablename>employee</tablename>
<count> 21</count>
<rt> 1</rt>
<rt> 2</rt>
<rt> 3</rt>
<rt> 4</rt>
</table>
<table>
<tablename>dept</tablename>
<count>20</count>
<rt> a</rt>
<rt> b</rt>
<rt> c</rt>
</table>
<table>
<tablename>dept</tablename>
<count>44</count>
<rt> d</rt>
<rt> e</rt>
<rt> g</rt>
</table>
</route>
</rpc>
</dynamic>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rpc="http://namespace/example">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Create a key matching "table" using "tablename" as the key.-->
<xsl:key name="table-by-name" match="rpc:table" use="rpc:tablename"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rpc:route">
<xsl:copy>
<!--Iterate over the first "table" for each key ("tablename").
The context is now "table".-->
<xsl:for-each
select="rpc:table[count(.|key('table-by-name',rpc:tablename)[1])=1]">
<xsl:copy>
<!--apply-templates to the "tablename" and "count" elements.
Also apply-templates to "rt" children of items with a key
that matches the current "tablename".-->
<xsl:apply-templates
select="rpc:tablename|rpc:count|key('table-by-name',rpc:tablename)/rpc:rt"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<dynamic>
<rpc xmlns="http://namespace/example">
<route>
<table>
<tablename>employee</tablename>
<count>20</count>
<rt> 1</rt>
<rt> 2</rt>
<rt> 3</rt>
<rt> 4</rt>
</table>
<table>
<tablename>dept</tablename>
<count>20</count>
<rt> a</rt>
<rt> b</rt>
<rt> c</rt>
<rt> d</rt>
<rt> e</rt>
<rt> g</rt>
</table>
</route>
</rpc>
</dynamic>
这篇关于xslt xml 第一次出现并合并标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文