ยกเลิกการกำหนดมาตรฐาน XML และจัดกลุ่มตามค่าของเด็กใน XSL

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

person Iosif Tanasescu    schedule 03.11.2014    source แหล่งที่มา
comment
คุณกำลังมองหาการแปลง XML เป็น XML หรือไม่? คุณได้พิจารณาแยกส่วนของ XML โดยใช้ XPath แทนหรือไม่ คุณสามารถให้รหัสใด ๆ และอธิบายว่ามีเจตนาอะไรที่นี่? ฉันคิดว่า XPath นั้นง่ายกว่ามาก แต่ฉันอาจเข้าใจผิดได้ ฉันต้องเข้าใจมากขึ้นว่าปัญหาใดที่ต้องแก้ไข อย่างไรก็ตาม คุณจะต้องใช้ XPath บน XSLT ของคุณ   -  person hfontanez    schedule 04.11.2014


คำตอบ (1)


ฉันคิดว่า* คุณต้องการบางอย่างเช่น:

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

--
(*) การย่อตัวอย่างให้เล็กสุดจะเป็นประโยชน์

person michael.hor257k    schedule 04.11.2014
comment
ขอบคุณไมเคิล มันดูเกือบจะสมบูรณ์แบบ สิ่งเดียวก็คือใน ListOfElementsToGroupBy เด็กทั้งหมดมีอยู่ ควรกรองโดยกลุ่มเท่านั้น จึงมีเด็กเพียงคนเดียว (นี่คือเหตุผลที่ฉันเอารายการ..group_me ออกในเอาต์พุตที่คาดหวังและรวม chlid เดียวเท่านั้น) - person Iosif Tanasescu; 04.11.2014
comment
@IosifTanasescu นั่นไม่ใช่เรื่องง่าย คุณแน่ใจหรือไม่ว่าต้องการสิ่งนี้? กลุ่มนี้เป็นที่รู้จักโดยกลุ่มผู้ปกครอง (ใหม่) - person michael.hor257k; 04.11.2014
comment
ฉันกำลังพยายามใช้มันตามที่เป็นอยู่ (โดยหลักแล้วฉันกำลังทำการเปลี่ยนแปลงนี้และพยายามวางไว้หน้ารายงาน BIPublisher ซึ่งเรียกใช้จาก Siebel) ตามที่คุณพูดถูก ฉันมีค่ากลุ่มซึ่งเป็นสิ่งที่ฉันต้องการจริงๆ จากเด็กคนนั้น ข้อมูลบรรพบุรุษทั้งหมดได้รับการลดขนาดอย่างถูกต้อง ขอบคุณอีกครั้งไมเคิล - person Iosif Tanasescu; 04.11.2014
comment
@IosifTanasescu หากคุณพบว่าคุณต้องการมัน นี่คือคำตอบที่ฉันโพสต์ในวันนี้เพื่อความต้องการที่คล้ายกันมาก: stackoverflow.com/questions /26737590/; มิฉะนั้น หากคำถามของคุณได้รับคำตอบแล้ว โปรดปิดโดยการยอมรับคำตอบ - person michael.hor257k; 04.11.2014