简体   繁体   English

在XSLT中按属性值对XML节点进行分组

[英]Grouping XML nodes by attribute value in XSLT

Im quite new to xslt transforms and I need help with one kind of transformation. 我对xslt转换非常陌生,我需要一种转换的帮助。 I need to group all nodes of certain type by one of it atributes and list parents of every kind of attribute. 我需要将某种类型的所有节点按属性之一分组,并列出每种属性的父级。 It is a kind of making summary of usage of certain things in the document. 这是对文档中某些内容的用法进行汇总的一种。 The I will present simplified example. 我将提供简化的示例。
Input: 输入:

<root>
<node name="node1">
    <somechild child-id="1">
</node>
<node name="node2">
    <somechild child-id="2">
</node>
<node name="node3">
    <somechild child-id="1">
</node>
<node name="node4">
    <somechild child-id="2">
</node>
<node name="node5">
    <somechild child-id="3">
</node>
</root>

Desired output: 所需的输出:

<root>
<somechild child-id="1">
    <is-child-of>
        <node name="node1" />
        <node name="node3" />
    </is-child-of>
</somechild>
<somechild child-id="2">
    <is-child-of>
        <node name="node2" />
        <node name="node4" />
    </is-child-of>
</somechild>
<somechild child-id="3">
    <is-child-of>
        <node name="node5" />
    </is-child-of>
</somechild>
</root>

Idea is that if is the same element in many nodes they have same child-id. 想法是,如果许多节点中的元素相同,则它们具有相同的子ID。 I need to find all used by every . 我需要找到每个人都用过的东西。 I found this question XSLT transformation to xml, grouping by key which is kind of similar but there is a declaration of all authors on the begining and I don't have such, is allways only a child of . 我发现这个问题从XSLT到xml的转换,按键进行分组,这有点相似,但是一开始就声明了所有作者,但我没有这样的声明,始终只是的子级。

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

    <xsl:key name="k" match="somechild" use="@child-id"/>
    <xsl:key name="n" match="node" use="somechild/@child-id"/>

    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates 
                select="//somechild[generate-id(.) = generate-id(key('k', @child-id))]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="somechild">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>

            <is-child-of>
                <xsl:apply-templates select="key('n', @child-id)"/>
            </is-child-of>
        </xsl:copy>

    </xsl:template>

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

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

</xsl:stylesheet>

Output: 输出:

<root>
  <somechild child-id="1">
    <is-child-of>
      <node name="node1" />
      <node name="node3" />
    </is-child-of>
  </somechild>
  <somechild child-id="2">
    <is-child-of>
      <node name="node2" />
      <node name="node4" />
    </is-child-of>
  </somechild>
  <somechild child-id="3">
    <is-child-of>
      <node name="node5" />
    </is-child-of>
  </somechild>
</root>

You could try the following approach? 您可以尝试以下方法吗?

<!-- select the current child id to filter by -->
<xsl:variable name="id" select="somechild/@child-id"/>
<!-- select the nodes which have a somechild element with the child-id to look for -->
<xsl:for-each select="/root//some-child[@child-id = $id]/..">
   <!-- for each such node, do something -->
</xsl:for-each>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM