【问题标题】:xslt xml getting first occurrence and merging the tagsxslt xml第一次出现并合并标签
【发布时间】:2017-10-31 16:24:06
【问题描述】:

下面是 XML,我正在寻找如下输出

输入 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>

请注意,只需要选择第一个&lt;count&gt;

输出 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>          

【问题讨论】:

  • 您应该提供您尝试使用的 xslt 以及您遇到的任何错误

标签: xml xslt xml-parsing xslt-1.0 xslt-grouping


【解决方案1】:

一个使用muenchian grouping的选项...

编辑处理默认命名空间。

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>

【讨论】:

  • 如果我有一个如下的命名空间,并且我希望它位于 标记处,并且我需要其余的功能才能按上述方式工作。我的代码看起来如何&lt;dynamic&gt; &lt;rpc xmlns="http://namespace/example &gt; &lt;route&gt; &lt;table&gt;
  • 你能回答我在命名空间上编辑过的问题吗
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 2021-10-02
  • 2010-10-16
  • 2015-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多