首页 文章

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

提问于
浏览
2

我想改造

<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接近回答这个问题,Group/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>

提前致谢!

2 回答

  • 1

    This transformation:

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

    when applied on the provided XML document:

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

    produces the wanted, correct result:

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

    Explanation


    II. In case all values must be concatenated together, use this slightly modified solution:

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

    这是一个稍微不同的解决方案,在我注意到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关于' - '作为分隔符的注释,而不是第一个和最后一个值之间的分隔符 .

相关问题