在XSL中对XML进行Denormilize并按值分组

时间:2014-11-03 23:48:49

标签: xslt-1.0

我打算"反规范化"并按子元素的值分组。
尝试:识别组(基于GoupID元素)
         创建输出组元素,包含初始父

中的所有相关信息

初始样本(真正的样本确实包含更多元素)是:

<?xml version="1.0" encoding="UTF-8"?>
<ListOfBIPIncident>
    <Incident>
        <IncidentId>1</IncidentId>
        <Element1>Value of element 1 in incidentId = 1 </Element1>
        <Element2>Value of element 2 in incidentId = 1 </Element2>
        <ListOfDetails>
            <Space1>Space of 1</Space1>
            <Space2>Space of 2</Space2>
        </ListOfDetails>
        <ListOfElementsToGroupBy>
            <Group_Me>
                <GroupID>100</GroupID>
                <GroupName>Name of group 100</GroupName>
                <DateOf>12/12/2002</DateOf>
            </Group_Me>
            <Group_Me>
                <GroupID>101</GroupID>
                <GroupName>Name of group 101</GroupName>
                <DateOf>1/1/2012</DateOf>
            </Group_Me>
        </ListOfElementsToGroupBy>
        <ListOfOtherDetails>
            <Other1>Which One</Other1>
            <Other2>Which Two</Other2>
        </ListOfOtherDetails>
    </Incident>
    <Incident>
        <IncidentId>3</IncidentId>
        <Element1>Value of element 1 in incidentId = 3 </Element1>
        <Element2>Value of element 2 in incidentId = 3 </Element2>
        <ListOfDetails>
            <Space1>Space of 1 3</Space1>
            <Space2>Space of 2 3</Space2>
        </ListOfDetails>
        <ListOfElementsToGroupBy>
            <Group_Me>
                <GroupID>301</GroupID>
                <GroupName>Name of group 301</GroupName>
                <DateOf>3/3/2003</DateOf>
            </Group_Me>
        </ListOfElementsToGroupBy>
        <ListOfOtherDetails>
            <Other1>Which One 3</Other1>
            <Other2>Which Two 3</Other2>
        </ListOfOtherDetails>
    </Incident>
    <Incident>
        <IncidentId>2</IncidentId>
        <Element1>Value of element 1 in incidentId = 2 </Element1>
        <Element2>Value of element 2 in incidentId = 2 </Element2>
        <ListOfDetails>
            <Space1>Space of 1 2</Space1>
            <Space2>Space of 2 2</Space2>
        </ListOfDetails>
        <ListOfElementsToGroupBy>
            <Group_Me>
                <GroupID>101</GroupID>
                <GroupName>Name of group 101</GroupName>
                <DateOf>2/2/2009</DateOf>
            </Group_Me>
            <Group_Me>
                <GroupID>401</GroupID>
                <GroupName>Name of group 401</GroupName>
                <DateOf>5/5/2039</DateOf>
            </Group_Me>
        </ListOfElementsToGroupBy>
        <ListOfOtherDetails>
            <Other1>Which One 2</Other1>
            <Other2>Which Two 2</Other2>
        </ListOfOtherDetails>
    </Incident>
</ListOfBIPIncident>

预期结果:

<ListOfBIPIncident>
  <group name="100">
    <Incident>
      <IncidentId>1</IncidentId>
      <Element1>Value of element 1 in incidentId = 1</Element1>
      <Element2>Value of element 2 in incidentId = 1</Element2>
      <ListOfDetails>
        <Space1>Space of 1</Space1>
        <Space2>Space of 2</Space2>
      </ListOfDetails>
      <Group_Me>
        <GroupID>100</GroupID>
        <GroupName>Name of group 100</GroupName>
        <DateOf>12/12/2002</DateOf>
      </Group_Me>
      <ListOfOtherDetails>
        <Other1>Which One</Other1>
        <Other2>Which Two</Other2>
      </ListOfOtherDetails>
    </Incident>
  </group>
  <group name="101">
    <Incident>
      <IncidentId>1</IncidentId>
      <Element1>Value of element 1 in incidentId = 1</Element1>
      <Element2>Value of element 2 in incidentId = 1</Element2>
      <ListOfDetails>
        <Space1>Space of 1</Space1>
        <Space2>Space of 2</Space2>
      </ListOfDetails>
      <Group_Me>
        <GroupID>101</GroupID>
        <GroupName>Name of group 101</GroupName>
        <DateOf>1/1/2012</DateOf>
      </Group_Me>
      <ListOfOtherDetails>
        <Other1>Which One</Other1>
        <Other2>Which Two</Other2>
      </ListOfOtherDetails>
    </Incident>
    <Incident>
      <IncidentId>2</IncidentId>
      <Element1>Value of element 1 in incidentId = 2</Element1>
      <Element2>Value of element 2 in incidentId = 2</Element2>
      <ListOfDetails>
        <Space1>Space of 1 2</Space1>
        <Space2>Space of 2 2</Space2>
      </ListOfDetails>
      <Group_Me>
        <GroupID>101</GroupID>
        <GroupName>Name of group 101</GroupName>
        <DateOf>2/2/2009</DateOf>
      </Group_Me>
      <ListOfOtherDetails>
        <Other1>Which One 2</Other1>
        <Other2>Which Two 2</Other2>
      </ListOfOtherDetails>
    </Incident>
  </group>
  <group name="301">
    <Incident>
      <IncidentId>3</IncidentId>
      <Element1>Value of element 1 in incidentId = 3</Element1>
      <Element2>Value of element 2 in incidentId = 3</Element2>
      <ListOfDetails>
        <Space1>Space of 1 3</Space1>
        <Space2>Space of 2 3</Space2>
      </ListOfDetails>
      <Group_Me>
        <GroupID>301</GroupID>
        <GroupName>Name of group 301</GroupName>
        <DateOf>3/3/2003</DateOf>
      </Group_Me>
      <ListOfOtherDetails>
        <Other1>Which One 3</Other1>
        <Other2>Which Two 3</Other2>
      </ListOfOtherDetails>
    </Incident>
  </group>
  <group name="401">
    <Incident>
      <IncidentId>2</IncidentId>
      <Element1>Value of element 1 in incidentId = 2</Element1>
      <Element2>Value of element 2 in incidentId = 2</Element2>
      <ListOfDetails>
        <Space1>Space of 1 2</Space1>
        <Space2>Space of 2 2</Space2>
      </ListOfDetails>
      <Group_Me>
        <GroupID>401</GroupID>
        <GroupName>Name of group 401</GroupName>
        <DateOf>5/5/2039</DateOf>
      </Group_Me>
      <ListOfOtherDetails>
        <Other1>Which One 2</Other1>
        <Other2>Which Two 2</Other2>
      </ListOfOtherDetails>
    </Incident>
  </group>
</ListOfBIPIncident>

我确实开始了类似下面的内容,这远远不是我需要的东西(它必须是xsl的1.0版本):

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" encoding="UTF-8"  indent="yes" omit-xml-declaration="yes" />

  <xsl:key name="kEyMe" match="Group_Me"  use="GroupID" />

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

<xsl:template match="Group_Me[generate-id()=generate-id (key('kEyMe',GroupID)[1])]">

      <group name="{GroupID}">
        <xsl:copy-of select="key('kEyMe',GroupID)" />
      </group>
    </xsl:template>

    <xsl:template match="Group_Me[not(generate-id()=generate-id(key('kEyMe',GroupID)[1]))]" />


</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我认为*你想要的东西:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="group-by-id" match="Group_Me" use="GroupID"/>
<xsl:key name="incident-by-group" match="Incident" use="ListOfElementsToGroupBy/Group_Me/GroupID"/>

<xsl:template match="/ListOfBIPIncident">
    <ListOfBIPIncident>
        <xsl:for-each select="Incident/ListOfElementsToGroupBy/Group_Me[generate-id()=generate-id (key('group-by-id', GroupID)[1])]">
            <group name="{GroupID}">
                <xsl:copy-of select="key('incident-by-group', GroupID)" />
            </group>
        </xsl:for-each>
    </ListOfBIPIncident>
</xsl:template>

</xsl:stylesheet>

或者,如果您愿意:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="group-by-id" match="Group_Me" use="GroupID"/>

<xsl:template match="/ListOfBIPIncident">
    <ListOfBIPIncident>
        <xsl:for-each select="Incident/ListOfElementsToGroupBy/Group_Me[generate-id()=generate-id (key('group-by-id',GroupID)[1])]">
            <group name="{GroupID}">
                <xsl:copy-of select="key('group-by-id',GroupID)/ancestor::Incident" />
            </group>
        </xsl:for-each>
    </ListOfBIPIncident>
</xsl:template>

</xsl:stylesheet>

-
(*)尽量减少这个例子会有所帮助。