1

我正在尝试将仅在其父元素内的子元素分组到它们自己的父分组中。

按 action/@publishForResources 分组,但在其自己的父元素操作中。

所以如果我有一个如下的 XML

<?xml version="1.0" encoding="UTF-8"?>
<ScheduleUpdates id_schedule="10" timestamp="2013-07-31T09:28:15.285+10:00" id_user="2" UserId="2" UserName="admin" caused-by="" CookiePGS="" CRBSTransactionID="2298">
    <actions operation="insert">
        <action id_action="207836" actionKindId="3" id_task="4094" publishForResources="855"/>
        <action id_action="207824" actionKindId="3" id_task="4101" publishForResources="876"/>
        <action id_action="207828" actionKindId="3" id_task="4098" publishForResources="876"/>
        <action id_action="207817" actionKindId="3" id_task="4097" publishForResources="876"/>
        <action id_action="207840" actionKindId="3" id_task="4095" publishForResources="855"/>
    </actions>
    <actions operation="update">
        <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="810"/>
        <action id_action="207824" actionKindId="3" id_task="3101" publishForResources="872"/>
        <action id_action="207828" actionKindId="3" id_task="3098" publishForResources="872"/>
        <action id_action="207817" actionKindId="3" id_task="3097" publishForResources="872"/>
        <action id_action="207840" actionKindId="3" id_task="3095" publishForResources="810"/>
    </actions>
    <actions operation="delete">
        <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="73"/>
        <action id_action="207824" actionKindId="3" id_task="2101" publishForResources="55"/>
        <action id_action="207828" actionKindId="3" id_task="2098" publishForResources="55"/>
        <action id_action="207817" actionKindId="3" id_task="2097" publishForResources="73"/>
        <action id_action="207840" actionKindId="3" id_task="2095" publishForResources="73"/>
    </actions>
</ScheduleUpdates>

结果应该是

<?xml version="1.0" encoding="UTF-8"?>
<ScheduleUpdates id_schedule="10" timestamp="2013-07-31T09:28:15.285+10:00" id_user="2" UserId="2" UserName="admin" caused-by="" CookiePGS="" CRBSTransactionID="2298">
    <actions operation="insert" publishForResources="876">
        <action id_action="207824" actionKindId="3" id_task="4101" publishForResources="876"/>
        <action id_action="207828" actionKindId="3" id_task="4098" publishForResources="876"/>
        <action id_action="207817" actionKindId="3" id_task="4097" publishForResources="876"/>
    </actions>
    <actions operation="insert" publishForResources="855">
        <action id_action="207836" actionKindId="3" id_task="4094" publishForResources="855"/>
        <action id_action="207840" actionKindId="3" id_task="4095" publishForResources="855"/>
    </actions>
    <actions operation="update" publishForResources="810">
        <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="810"/>
        <action id_action="207840" actionKindId="3" id_task="3095" publishForResources="810"/>
    </actions>
    <actions operation="update" publishForResources="872">
        <action id_action="207824" actionKindId="3" id_task="3101" publishForResources="872"/>
        <action id_action="207828" actionKindId="3" id_task="3098" publishForResources="872"/>
        <action id_action="207817" actionKindId="3" id_task="3097" publishForResources="872"/>
    </actions>
    <actions operation="delete" publishForResources="73">
        <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="73"/>
        <action id_action="207817" actionKindId="3" id_task="2097" publishForResources="73"/>
        <action id_action="207840" actionKindId="3" id_task="2095" publishForResources="73"/>
    </actions>
    <actions operation="delete" publishForResources="55">
        <action id_action="207824" actionKindId="3" id_task="2101" publishForResources="55"/>
        <action id_action="207828" actionKindId="3" id_task="2098" publishForResources="55"/>
    </actions>
</ScheduleUpdates>

我一直在琢磨 XSLT 1.0 分组/xsl:key 解决方案,但我似乎无法让它工作。

感谢一些帮助和指导。

谢谢你的帮助

大卫

4

1 回答 1

0

这个怎么样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kPubFor" match="action"
           use="concat(../@operation, '+', @publishForResources)" />

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

  <xsl:template match="actions">
    <xsl:apply-templates 
      select="action[generate-id() = 
                     generate-id(key('kPubFor',
                                     concat(../@operation, '+',
                                     @publishForResources))[1])]"
      mode="group" />
  </xsl:template>

  <xsl:template match="action" mode="group">
    <actions>
      <xsl:apply-templates select="../@*" />
      <xsl:attribute name="publishForResources">
        <xsl:value-of select="@publishForResources" />
      </xsl:attribute>

      <xsl:apply-templates select="key('kPubFor', 
                                       concat(../@operation, '+',
                                              @publishForResources))" />
    </actions>
  </xsl:template>

</xsl:stylesheet>

在您的示例输入上运行时,结果是:

<ScheduleUpdates id_schedule="10" timestamp="2013-07-31T09:28:15.285+10:00" id_user="2" UserId="2" UserName="admin" caused-by="" CookiePGS="" CRBSTransactionID="2298">
  <actions operation="insert" publishForResources="855">
    <action id_action="207836" actionKindId="3" id_task="4094" publishForResources="855" />
    <action id_action="207840" actionKindId="3" id_task="4095" publishForResources="855" />
  </actions>
  <actions operation="insert" publishForResources="876">
    <action id_action="207824" actionKindId="3" id_task="4101" publishForResources="876" />
    <action id_action="207828" actionKindId="3" id_task="4098" publishForResources="876" />
    <action id_action="207817" actionKindId="3" id_task="4097" publishForResources="876" />
  </actions>
  <actions operation="update" publishForResources="810">
    <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="810" />
    <action id_action="207840" actionKindId="3" id_task="3095" publishForResources="810" />
  </actions>
  <actions operation="update" publishForResources="872">
    <action id_action="207824" actionKindId="3" id_task="3101" publishForResources="872" />
    <action id_action="207828" actionKindId="3" id_task="3098" publishForResources="872" />
    <action id_action="207817" actionKindId="3" id_task="3097" publishForResources="872" />
  </actions>
  <actions operation="delete" publishForResources="73">
    <action id_action="207836" actionKindId="3" id_task="2094" publishForResources="73" />
    <action id_action="207817" actionKindId="3" id_task="2097" publishForResources="73" />
    <action id_action="207840" actionKindId="3" id_task="2095" publishForResources="73" />
  </actions>
  <actions operation="delete" publishForResources="55">
    <action id_action="207824" actionKindId="3" id_task="2101" publishForResources="55" />
    <action id_action="207828" actionKindId="3" id_task="2098" publishForResources="55" />
  </actions>
</ScheduleUpdates>
于 2013-07-31T08:23:40.553 回答