如何合并具有“相同父亲"、相同方法和相同 id=0 的两个节点(使用 XSLT)? [英] how to merge two nodes having "the same father", the same method and the same id=0 (using XSLT)?

查看:22
本文介绍了如何合并具有“相同父亲"、相同方法和相同 id=0 的两个节点(使用 XSLT)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并两个节点 street,因为它们具有相同的:

I need to merge the two nodes street as they have the same:

  • 父节点:city NEW YORK
  • 同样的方法:修改
  • 相同的 ID:0

必须合并属性值(请参阅本文末尾的输出文件)

Attributes values must be merged (See the output file at the end of this post)

这是输入文件:

<country>
<state id="NEW JERSEY">
    <city id="NEW YORK">
     <district id="BRONX" method="modify">

        <street id="0" method="modify">
           <attributes>
              <temperature>98</temperature>
              <altitude>1300</altitude>
           </attributes>
        </street>

        <dadada id="99" method="modify" />

        <street id="0" method="modify">
           <attributes>
              <temperature>80</temperature>
              <streetnumber> 67 </streetnumber>
           </attributes>
        </street>

        <dididi id="432" method="modify" />

     </district>


  </city>

</state>

预期输出:

<country>
<state id="NEW JERSEY">
    <city id="NEW YORK">
     <district id="BRONX" method="modify">

        <street id="0" method="modify">
           <attributes>
              <temperature>80</temperature>
              <altitude>1300</altitude>
              <streetnumber> 67 </streetnumber>
           </attributes>
        </street>

        <dadada id="99" method="modify" />

        <dididi id="432" method="modify" />

     </district>

  </city>

</state>
</country>

请帮忙,我刚刚开始 XSLT

Please help, I am just beginning XSLT

推荐答案

我假设您对 XSLT 2.0 感兴趣,因为这是您标记问题的方式.如果您需要等效的 XSLT 1.0,请告诉我.这个 XSLT 2.0 样式表应该可以解决问题......

I have assumed that you are interested in an XSLT 2.0, because that is how you have tagged your question. Let me know if you need the XSLT 1.0 equivalent. This XSLT 2.0 style-sheet should do the trick ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

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

<xsl:template match="*[street]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:for-each-group select="street" group-by="@method">
    <xsl:apply-templates select="current-group()[1]" />
   </xsl:for-each-group>
   <xsl:apply-templates select="node()[not(self::street)]"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="street/attributes">
 <xsl:copy>
  <xsl:apply-templates select="@*"/>
   <xsl:variable name="grouped-method" select="../@method" />  
   <xsl:for-each-group select="../../street[@method=$grouped-method]/attributes/*" group-by="name()">
    <xsl:apply-templates select="current-group()[1]" />
   </xsl:for-each-group>    
  <xsl:apply-templates select="comment()|processing-instruction()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet> 

说明

第二个模板,匹配街道的父元素,将按照通用方法对子街道进行分组.对于每个组,仅复制组中的第一条街道.其余的被丢弃.

Explanation

The second template, matching on elements which are parents of streets, will group the child streets by common method. For each group, only the first street in the group is copied. The rest are dropped.

当该组的第一条街道在第三个模板中具有其属性"节点过程时,我们合并来自同一组的所有属性.可能属性"是 XML 文档中的一个不幸的元素名称!这种分组是通过查看具有相同街道父级(布朗克斯区)的所有联合街道的所有属性"子节点并按元素名称分组来实现的.如果这样一个组中有多个元素,只需取第一个的值.

When this first street of the group is having its 'attributes' node process in the third template, we merge all the attributes from the same group. Possibly 'attributes' is an unfortunate element name in an XML document! This grouping is achieved by looking at all the 'attributes' child nodes of all the allied streets which have the same street parent (Bronx district) and grouping by element name. If there are multiple elements in such a group, just take the values from the first one.

我不确定这是否正是您想要的,因为尽管街道属性由父"节点(布朗克斯)合并,但它们并未在城市级别合并.这反映了您问题中的歧义.样本日期中街道的父"节点是地区而不是城市.如果我弄错了,并且您想在城市级别进行分组,请澄清并更新您的问题.

I'm not sure that this is exactly what you want because although the street attributes are merged by 'father' node (Bronx), they are not merged at the city level. This reflects the ambiguity in your question. The 'father' node of streets in your sample date is district not city. If I have got this wrong, and you want grouping at the city level, please clarify and update your question.

这篇关于如何合并具有“相同父亲"、相同方法和相同 id=0 的两个节点(使用 XSLT)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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