XSLT 1.0 - 连接已知的子节点,按未知父节点分组

时间:2012-08-26 23:58:08

标签: xslt xslt-grouping

我想转型

    <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 XSLT接近回答问题,{​​{3}}也是如此,但它们并不是我所需要的。

到目前为止,我有:

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

提前致谢!

2 个答案:

答案 0 :(得分: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关于' - '作为分隔符的注释,而不是第一个和最后一个值之间的分隔符。

答案 1 :(得分:0)

此转化:

<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


  3. <强> II。如果必须将所有值连接在一起,请使用此略微修改的解决方案:

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