合并具有相同值的多个属性节点 [英] merging multiple attribute nodes with same value

查看:38
本文介绍了合并具有相同值的多个属性节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我看看如何做这个xml:xml看起来像

Can you please help me on how to do this xml: The xml looks like

    <a name="hr_1" id="hr">
    <text>11</text>
    </a>
    <a name="hr_2" id="hr">
    <text>12</text>
    </a>

    <a name="hre_1" id ="hre">
    <text>11</text>
    </a>
    <a name="hre_2" id ="hre">
    <text>12</text>
    </a>

预期输出:转换后的输出如下

expected output:The transformed output is expected like below

    <b name ="hr">
    <value>11</value>
    <value>12</value>
    </b>

    <b name ="hre">
    <value>11</value>
    <value>12</value>
    </b>

推荐答案

来自评论:

非常感谢...我如何在 xslt 1.0 中做到这一点...我还添加了一个更多标签id,所以我需要根据id进行分组.请在xslt 1.0中提供帮助

Thank you so much... How can i do it in xslt 1.0.. Also i added one more tag id, so i need to group based on id.Please help in xslt 1.0

在 XSLT 1.0 中,使用 慕尼黑分组.我要做的是创建一个匹配所有 text 元素并使用父元素的 id 属性的键...

In XSLT 1.0, use Muenchian Grouping. What I would do is create a key matching all text elements and using the id attribute of the parent...

XML

<doc>
    <a name="hr_1" id="hr">
        <text>11</text>
    </a>
    <a name="hr_2" id="hr">
        <text>12</text>
    </a>    
    <a name="hre_1" id ="hre">
        <text>11b</text>
    </a>
    <a name="hre_2" id ="hre">
        <text>12b</text>
    </a>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="kText" match="text" use="../@id"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="*/text[count(.|key('kText',../@id)[1])=1]">
        <b name="{../@id}">
          <xsl:apply-templates select="key('kText',../@id)"/>
        </b>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

输出

<doc>
   <b name="hr">
      <text>11</text>
      <text>12</text>
   </b>
   <b name="hre">
      <text>11b</text>
      <text>12b</text>
   </b>
</doc>

这篇关于合并具有相同值的多个属性节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆