2012-04-08 17 views
1

Wie würde ich die einzigartigen SectionLabels für jeden FormDef in der folgenden XML extrahieren:Extract einzigartiger Text aus mehreren überlappenden Elementen

<XML> 
<FormDef OID="F_TEST_1" Name="Test Form 1"> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_1" /> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_4" /> 
</FormDef> 
<FormDef OID="F_TEST_2" Name="Test Form 2"> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_2" /> 
</FormDef> 
<FormDef OID="F_TEST_3" Name="Test Form 3"> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_2"/> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_3"/> 
</FormDef> 
<FormDef OID="F_TEST_4" Name="Test Form 4"> 
    <ItemGroupRef ItemGroupOID="TEST_GROUP_4"/> 
</FormDef> 

<ItemGroupDef OID="TEST_GROUP_1" Name="Ungrouped"> 
    <ItemRef ItemOID="I_MSA1_INIT" /> 
    <ItemRef ItemOID="I_MSA1_FORMD" /> 
    <ItemRef ItemOID="I_MSA1_MSA1_CONS" /> 
    <ItemRef ItemOID="I_MSA1_MSA1_PGT" /> 
</ItemGroupDef> 
<ItemGroupDef OID="TEST_GROUP_2" Name="MSA1_complyreasG" > 
    <ItemRef ItemOID="I_MSA1_MSA1_NOELIGREAS" /> 
    <ItemRef ItemOID="I_MSA1_MSA1_COMPLYREAS" /> 
</ItemGroupDef> 
<ItemGroupDef OID="TEST_GROUP_3" Name="Ungrouped"> 
    <ItemRef ItemOID="I_MSA2_INIT" /> 
    <ItemRef ItemOID="I_MSA2_FROMD" /> 
    <ItemRef ItemOID="I_MSA2_IDV" /> 
    <ItemRef ItemOID="I_MSA2_MSA2_INITBF" /> 
</ItemGroupDef> 
<ItemGroupDef OID="TEST_GROUP_4" Name="MSA2_POARTprecG"> 
    <ItemRef ItemOID="I_MSA2_MSA2_POARTPREC" /> 
    <ItemRef ItemOID="I_MSA2_MSA2_POARTNBV" /> 
    <ItemRef ItemOID="I_MSA2_MSA2_LOARTPREC" /> 
</ItemGroupDef> 

<ItemDef OID="I_MSA1_INIT"> 
    <ItemDetails ItemOID="I_MSA1_INIT"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA1_FORMD"> 
    <ItemDetails ItemOID="I_MSA1_FORMD"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA1_MSA1_CONS"> 
    <ItemDetails ItemOID="I_MSA1_MSA1_CONS"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section3</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA1_MSA1_NOELIGREAS"> 
    <ItemDetails ItemOID="I_MSA1_MSA1_NOELIGREAS"> 
     <ItemPresentInForm FormOID="F_TEST_2"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
     <ItemPresentInForm FormOID="F_TEST_3"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA1_MSA1_COMPLYREAS"> 
    <ItemDetails ItemOID="I_MSA1_MSA1_COMPLYREAS"> 
     <ItemPresentInForm FormOID="F_TEST_2"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
     <ItemPresentInForm FormOID="F_TEST_3"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_INIT"> 
    <ItemDetails ItemOID="I_MSA2_INIT"> 
     <ItemPresentInForm FormOID="F_TEST_3"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_FROMD"> 
    <ItemDetails ItemOID="I_MSA2_FROMD"> 
     <ItemPresentInForm FormOID="F_TEST_3"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_IDV"> 
    <ItemDetails ItemOID="I_MSA2_IDV"> 
     <ItemPresentInForm FormOID="F_TEST_3"> 
      <SectionLabel>Section3</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_MSA2_POARTPREC"> 
    <ItemDetails ItemOID="I_MSA2_MSA2_POARTPREC"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
     <ItemPresentInForm FormOID="F_TEST_4"> 
      <SectionLabel>Section1</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_MSA2_POARTNBV"> 
    <ItemDetails ItemOID="I_MSA2_MSA2_POARTNBV"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
     <ItemPresentInForm FormOID="F_TEST_4"> 
      <SectionLabel>Section2</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
<ItemDef OID="I_MSA2_MSA2_LOARTPREC"> 
    <ItemDetails ItemOID="I_MSA2_MSA2_LOARTPREC"> 
     <ItemPresentInForm FormOID="F_TEST_1"> 
      <SectionLabel>Section3</SectionLabel> 
     </ItemPresentInForm> 
     <ItemPresentInForm FormOID="F_TEST_4"> 
      <SectionLabel>Section3</SectionLabel> 
     </ItemPresentInForm> 
    </ItemDetails> 
</ItemDef> 
</XML> 

Zuerst definiere ich einen Schlüssel:

<xsl:key name="labels" match="ItemDef/ItemDetails/ItemPresentInForm" use="@FormOID" /> 

es dann in einem verwenden wählen Sie wie folgt:

<xsl:variable name="sections" 
       select="//*[local-name()='ItemDef']/*[local-name()='ItemDetails']/*[local-name()='ItemPresentInForm'] 
          [generate-id() = generate-id(key('labels', @FormOID))]"> 

       <xsl:value-of select="./SectionLabel" /> 

      </xsl:variable> 

Aber dies zurück:

Section1
Section1
Section1
Section1

den Schlüssel ändern auf der SectionLabel aufschlagen:

<xsl:key name="labels" match="ItemDef/ItemDetails/ItemPresentInForm" use="SectionLabel" /> 

XSLT zur Transformation:

   <xsl:variable name="sections" 
       select="//*[local-name()='ItemDef']/*[local-name()='ItemDetails']/*[local-name()='ItemPresentInForm'] 
          [generate-id(.) = generate-id(key('labels', SectionLabel))]"> 

       <xsl:value-of select="./SectionLabel" /> 

      </xsl:variable> 

Returns dies:

Section1
Section2
Section3
Abschnitt4
Section1
Section2
Section3
Abschnitt4
Section1
Section2
Section3
Abschnitt4
Section1
Section2
Section3
Abschnitt4

dem alle Etiketten Abschnitt in dem Dokument ist, wenn ich über jeden FORMDEF bin Iterieren. Ich ziele darauf ab, eine Ausgabe zu erhalten, wo nur die Abschnittsbeschriftungen für das aktuelle FormDef extrahiert werden.

Das sollte erwartete Ausgabe sein:

Form OID="F_TEST_1" 
Labels: Section1, Section2, Section3 

Form OID="F_TEST_2" 
Labels: Section1, Section2 

Form OID="F_TEST_3" 
Labels: Section1, Section2, Section3 

Form OID="F_TEST_4" 
Labels: Section1, Section2, Section3 

Dank

+0

Also, was sollte das Ergebnis sein? Bitte bearbeiten Sie die Frage und geben Sie das gewünschte Ergebnis an. Erklären Sie verständlicher, welche Knoten/Werte Sie auswählen möchten. –

+0

Die Ausgabe, die Sie in Ihrer Antwort gaben, ist, was ich beabsichtigte. Es zeigt, dass Sie die Frage sehr gut verstanden haben. –

+0

Ich habe meine * rating power * verwendet, um dies abzuleiten. Ihre Frage wäre besser definiert, wenn Sie genau die gewünschte Ausgabe produziert hätten. Es ist eine gute Idee, die Frage zu bearbeiten, und vor allem, um alle wichtigen Informationen für Ihre zukünftigen Fragen bereitzustellen. –

Antwort

0

Diese Transformation:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:key name="kLabelsInForm" match="SectionLabel" 
    use="concat(../@FormOID, '+', .)"/> 

<xsl:template match="FormDef"> 
    Form OID="<xsl:value-of select="@OID"/>" 
    Labels: <xsl:text/> 

    <xsl:variable name="vOID" select="@OID"/> 
    <xsl:for-each select= 
    "../ItemDef/*/*/SectionLabel 
     [generate-id() 
     = 
     generate-id(key('kLabelsInForm', 
         concat($vOID, '+', .) 
         ) 
         [1] 
        ) 
     ] 
    "> 
    <xsl:value-of select="concat(., ' ')"/> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

wenn auf dem mitgelieferten XML-Dokument angewandt:

<XML> 
    <FormDef OID="F_TEST_1" Name="Test Form 1"> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_1" /> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_4" /> 
    </FormDef> 
    <FormDef OID="F_TEST_2" Name="Test Form 2"> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_2" /> 
    </FormDef> 
    <FormDef OID="F_TEST_3" Name="Test Form 3"> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_2"/> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_3"/> 
    </FormDef> 
    <FormDef OID="F_TEST_4" Name="Test Form 4"> 
     <ItemGroupRef ItemGroupOID="TEST_GROUP_4"/> 
    </FormDef> 
    <ItemGroupDef OID="TEST_GROUP_1" Name="Ungrouped"> 
     <ItemRef ItemOID="I_MSA1_INIT" /> 
     <ItemRef ItemOID="I_MSA1_FORMD" /> 
     <ItemRef ItemOID="I_MSA1_MSA1_CONS" /> 
     <ItemRef ItemOID="I_MSA1_MSA1_PGT" /> 
    </ItemGroupDef> 
    <ItemGroupDef OID="TEST_GROUP_2" Name="MSA1_complyreasG" > 
     <ItemRef ItemOID="I_MSA1_MSA1_NOELIGREAS" /> 
     <ItemRef ItemOID="I_MSA1_MSA1_COMPLYREAS" /> 
    </ItemGroupDef> 
    <ItemGroupDef OID="TEST_GROUP_3" Name="Ungrouped"> 
     <ItemRef ItemOID="I_MSA2_INIT" /> 
     <ItemRef ItemOID="I_MSA2_FROMD" /> 
     <ItemRef ItemOID="I_MSA2_IDV" /> 
     <ItemRef ItemOID="I_MSA2_MSA2_INITBF" /> 
    </ItemGroupDef> 
    <ItemGroupDef OID="TEST_GROUP_4" Name="MSA2_POARTprecG"> 
     <ItemRef ItemOID="I_MSA2_MSA2_POARTPREC" /> 
     <ItemRef ItemOID="I_MSA2_MSA2_POARTNBV" /> 
     <ItemRef ItemOID="I_MSA2_MSA2_LOARTPREC" /> 
    </ItemGroupDef> 
    <ItemDef OID="I_MSA1_INIT"> 
     <ItemDetails ItemOID="I_MSA1_INIT"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA1_FORMD"> 
     <ItemDetails ItemOID="I_MSA1_FORMD"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA1_MSA1_CONS"> 
     <ItemDetails ItemOID="I_MSA1_MSA1_CONS"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section3</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA1_MSA1_NOELIGREAS"> 
     <ItemDetails ItemOID="I_MSA1_MSA1_NOELIGREAS"> 
      <ItemPresentInForm FormOID="F_TEST_2"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
      <ItemPresentInForm FormOID="F_TEST_3"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA1_MSA1_COMPLYREAS"> 
     <ItemDetails ItemOID="I_MSA1_MSA1_COMPLYREAS"> 
      <ItemPresentInForm FormOID="F_TEST_2"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
      <ItemPresentInForm FormOID="F_TEST_3"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_INIT"> 
     <ItemDetails ItemOID="I_MSA2_INIT"> 
      <ItemPresentInForm FormOID="F_TEST_3"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_FROMD"> 
     <ItemDetails ItemOID="I_MSA2_FROMD"> 
      <ItemPresentInForm FormOID="F_TEST_3"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_IDV"> 
     <ItemDetails ItemOID="I_MSA2_IDV"> 
      <ItemPresentInForm FormOID="F_TEST_3"> 
       <SectionLabel>Section3</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_MSA2_POARTPREC"> 
     <ItemDetails ItemOID="I_MSA2_MSA2_POARTPREC"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
      <ItemPresentInForm FormOID="F_TEST_4"> 
       <SectionLabel>Section1</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_MSA2_POARTNBV"> 
     <ItemDetails ItemOID="I_MSA2_MSA2_POARTNBV"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
      <ItemPresentInForm FormOID="F_TEST_4"> 
       <SectionLabel>Section2</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
    <ItemDef OID="I_MSA2_MSA2_LOARTPREC"> 
     <ItemDetails ItemOID="I_MSA2_MSA2_LOARTPREC"> 
      <ItemPresentInForm FormOID="F_TEST_1"> 
       <SectionLabel>Section3</SectionLabel> 
      </ItemPresentInForm> 
      <ItemPresentInForm FormOID="F_TEST_4"> 
       <SectionLabel>Section3</SectionLabel> 
      </ItemPresentInForm> 
     </ItemDetails> 
    </ItemDef> 
</XML> 

erzeugt die gewünschte, korrekte Ergebnis (die einzigartigen SectionLabel Werte für jede Form):

Form OID="F_TEST_1" 
    Labels: Section1 Section2 Section3 

    Form OID="F_TEST_2" 
    Labels: Section1 Section2 

    Form OID="F_TEST_3" 
    Labels: Section1 Section2 Section3 

    Form OID="F_TEST_4" 
    Labels: Section1 Section2 Section3 

Erklärung:

  • Muench-Gruppierung Verfahren, bei dem die Elemente gruppiert werden sollen mit einem zusammengesetzten Schlüssel indiziert.

  • Jeder einzigartiger Abschnitt Wert in einer Form wird durch die Verkettung der Zeichenfolge SectionLabel Wertes und der String-Wert des Attributs FormOID der Mutter identifiziert.

  • Auf diese Weise erzeugt derselbe SectionLabel Wert in verschiedenen Formen verschiedene Schlüssel und wird nicht nur einmal gezählt.

Hier sind einige gute Ressourcen auf Muench Gruppierung:

+0

Du mein lieber Freund bist mein Held. Das funktioniert wie ein Zauber. Nur eine Bitte, könntest du in "Laiensprache" erklären, wie du die Abschnitte mit dem Schlüssel erzeugst? –

+0

@Kata: Gern geschehen. Ja, ich habe die Antwort mit einer ausführlicheren Erklärung aktualisiert. –

+0

danke für die Literatur - wie gesagt, du hast mir den Tag gerettet! –