Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:59:27+00:00 2026-06-01T16:59:27+00:00

How would I extract the unique SectionLabels for each FormDef in the following XML:

  • 0

How would I extract the unique SectionLabels for each FormDefin the following 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>

First I define a key:

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

Then use it in a select like so:

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

But this returns:

Section1
Section1
Section1
Section1

Changing the key to pitch on the SectionLabel:

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

XSLT to transform:

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

Section1
Section2
Section3
Section4
Section1
Section2
Section3
Section4
Section1
Section2
Section3
Section4
Section1
Section2
Section3
Section4

Which is all the section labels in the document when I am iterating over each FormDef. I am aiming to get an output where only the section labels for the current FormDef are extracted.

The expected output should be:

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 

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:59:28+00:00Added an answer on June 1, 2026 at 4:59 pm

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

    when applied on the provided XML document:

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

    produces the wanted, correct result (the unique SectionLabel values for each 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 
    

    Explanation:

    • Muenchian grouping method where the elements to be grouped are indexed using a composite key.

    • Each unique section value in a form is identified by the concatenation of the SectionLabel string value and the string value of the FormOID attribute of its parent.

    • In this way the same SectionLabel value in different forms produces different keys and isn’t counted just once.

    Here are some good resources on Muenchian grouping:

    • Jeni Tennison’s grouping page.

    • Dave Pawson's XSLT FAQ's grouping page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to develop an application that would extract the following things from an
How would I extract the dollar amount from the following string some text will
I would like to extract only tar.gz from the following strings. /root/abc/xzy/file_tar_src-5.2.8.23.tar.gz or /root/abc/xyz/file_tar_src-5.2.tar.gz
Hello! I would like to extract all citations from a text. Additionally, the name
I would like to extract unique values from my (dynamically allocated) array. I have
How would I extract the time from a series of POSIXct objects discarding the
I would like to extract portion of a text using a regular expression. So
I would like to extract items from this sample html, more specificly, i would
I would like to extract the GPS EXIF tag from pictures using php. I'm
I would like to extract a value or its inverse from database. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.