【问题标题】:XSLT 1.0 - Concatenate known child nodes, group by unknown parentXSLT 1.0 - 连接已知子节点,按未知父节点分组
【发布时间】:2023-03-18 17:44:02
【问题描述】:

我想变身

    <entry>
        <parent1>
            <object_id>1580</object_id>
        </parent1>
        <parent1>
            <object_id>1586</object_id>
        </parent1>
        <parent2>
            <object_id>1582</object_id>
        </parent2>
        <parent2>
            <object_id>1592</object_id>
        </parent2>
    </entry>

进入

    <entry>
        <parent1>1580-1586</parent1>
        <parent2>1582-1592</parent2>
    </entry>

顶级条目名称未知。父节点名称未知,同名父节点的数量可能会有所不同。 子节点称为“object_id”。

所以,我想以抽象的方式对未知的父节点进行分组,并将子节点值连接起来,用“-”分隔。

Merge XML nodes using XSLTGroup/merge childs of same nodes in xml/xslt 几乎可以回答这个问题,但它们并不是我所需要的。

到目前为止我有:

    <xsl:key name="groupName" match="*[object_id]" use="."/>
    <xsl:template match="*[generate-id(.) = generate-id(key('groupName', .))]">
        <xsl:copy>
        <xsl:call-template name="join"> 
                <xsl:with-param name="list" select="object_id" /> 
                <xsl:with-param name="separator" select="'-'" />                                             
        </xsl:call-template>
        </xsl:copy> 
    </xsl:template>

    <xsl:template name="join"> 
    <xsl:param name="list" /> 
    <xsl:param name="separator"/>     
    <xsl:for-each select="$list"> 
      <xsl:value-of select="." /> 
      <xsl:if test="position() != last()"> 
        <xsl:value-of select="$separator" />         
      </xsl:if> 
    </xsl:for-each> 
    </xsl:template>

提前致谢!

【问题讨论】:

  • 您的密钥的 Use 属性需要是父名称,而不是 object_id 文本。这就是您要分组的内容:父母姓名。
  • 匹配/使用组合不能实现这一点吗?匹配 = 子节点为 'object_id' 的所有节点;使用 = '节点本身'。还是我需要使用'name()'?
  • 没有。使用=“。”结果键的值是匹配节点的字符串值,不是节点的名称。是的,您需要使用 name() 或 local-name(),具体取决于您的数据。

标签: xslt xslt-grouping


【解决方案1】:

这是一个稍微不同的解决方案,在我注意到 Dimitre 的帖子之前开发。

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

<xsl:key name="kParents" match="*[object_id]" use="local-name()" />

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

<xsl:template match="*[*/object_id]">
  <xsl:variable name="grandparent-id" select="generate-id()" /> 
 <xsl:copy>
   <xsl:apply-templates select="@* | node()[not(object_id)] |
    *[generate-id()=
      generate-id(
        key('kParents',local-name())[generate-id(..)=$grandparent-id][1])]"
      mode="group-head" />
 </xsl:copy>
</xsl:template>

<xsl:template match="*[object_id]" mode="group-head">
 <xsl:variable name="grandparent-id" select="generate-id(..)" /> 
 <xsl:copy>
   <xsl:apply-templates select="@* | node()[not(self::object_id)]" />
   <xsl:for-each select="key('kParents',local-name())[generate-id(..)=$grandparent-id]/object_id">
     <xsl:value-of select="." />
     <xsl:if test="position() != last()"> - </xsl:if>  
   </xsl:for-each>  
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

更新

我更新了样式表以反映 OP 关于“-”作为分隔符的评论,而不是第一个值和最后一个值之间的分隔符。

【讨论】:

    【解决方案2】:

    这种转变:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kObjByValAndParent" match="object_id"
      use="name(..)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*/*"/>
    
     <xsl:template priority="2" match=
     "/*/*[generate-id(object_id)
          =
           generate-id(key('kObjByValAndParent',name())[1])
          ]
     ">
       <xsl:copy>
         <xsl:value-of select=
         "concat(object_id, ' - ',
                 key('kObjByValAndParent',name())[last()]
                )
         "/>
       </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <entry>
        <parent1>
            <object_id>1580</object_id>
        </parent1>
        <parent1>
            <object_id>1586</object_id>
        </parent1>
        <parent2>
            <object_id>1582</object_id>
        </parent2>
        <parent2>
            <object_id>1592</object_id>
        </parent2>
    </entry>
    

    产生想要的正确结果:

    <entry>
       <parent1>1580 - 1586</parent1>
       <parent2>1582 - 1592</parent2>
    </entry>
    

    解释

    1. 正确使用和覆盖identity rule

    2. 正确使用Muenchian grouping method


    二。如果必须将所有值连接在一起,请使用这个稍作修改的解决方案:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kObjByValAndParent" match="object_id"
      use="name(..)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*/*"/>
    
     <xsl:template priority="2" match=
     "/*/*[generate-id(object_id)
          =
           generate-id(key('kObjByValAndParent',name())[1])
          ]
     ">
       <xsl:copy>
         <xsl:for-each select="key('kObjByValAndParent',name())">
          <xsl:if test="not(position()=1)"> - </xsl:if>
          <xsl:value-of select="."/>
         </xsl:for-each>
       </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 你是救生员!谢谢迪米特!!
    • concat() 看起来只需要 2 个同名的父母 - 可变数量的父母呢?
    • @gbentley,你很困惑——一个节点最多有一个父节点。
    • 抱歉,我的解释不正确。在我的问题中,我提到“父名称未知,同名父节点的数量可能会有所不同” - 因此连接可变数量的 object_id 值,而不仅仅是 2。
    • @gbentley,我将 x-y 解释为从 x 到 y 的区间。如果这是一个分隔符并且您想要所有值,由它分隔,请告诉我,我会立即修改解决方案 - 这很简单。
    猜你喜欢
    • 2021-12-05
    • 2021-03-14
    • 1970-01-01
    • 2017-12-06
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多