XSLT1.0按子属性值对节点进行分组



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

按操作/@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>

我一直在为XSLT1.0分组.xsl:key解决方案绞尽脑汁,但我似乎无法使其发挥作用。

感谢您的帮助和指导。

感谢您的帮助

David

这是怎么回事:

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

相关内容

  • 没有找到相关文章

最新更新