ฉันตั้งใจที่จะ "ทำให้ปกติ" และจัดกลุ่มตามค่าขององค์ประกอบในเด็ก
พยายาม: ระบุกลุ่ม (ตามองค์ประกอบ 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>
ฉันได้เริ่มต้นสิ่งต่อไปนี้ซึ่งยังห่างไกลจากสิ่งที่ฉันต้องการ (และจะต้องอยู่ในเวอร์ชัน 1.0 ของ xsl):
<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>