1

我将如何提取以下 XMLSectionLabels中每个的唯一性:FormDef

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

首先我定义一个键:

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

然后像这样在选择中使用它:

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

但这会返回:


1 节 第 1 节 第 1 节
第 1

更改键以在SectionLabel:

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

XSLT 进行转换:

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

返回这个:

第1
节2

3
节4
节1
节2节3节4节1节2节3 节
4
节1
节2

3
节 4



当我遍历每个 FormDef 时,这是文档中的所有部分标签。我的目标是获得一个输出,其中仅提取当前 FormDef 的部分标签。

预期的输出应该是:

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 

谢谢

4

1 回答 1

0

这种转变

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

应用于提供的 XML 文档时

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

产生想要的正确结果(每种形式的唯一SectionLabel值):

  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 

说明

  • Muenchian 分组方法,其中要分组的元素使用复合键进行索引。

  • 表单中的每个唯一节值都由字符串值及其父属性的SectionLabel字符串值的串联来标识。FormOID

  • 这样SectionLabel,不同形式的相同值会产生不同的键,并且不会只计算一次。

这里有一些关于 Muenchian 分组的好资源

于 2012-04-08T23:10:32.577 回答