1

need to Extract Values from all levels of Recursive XML structure.The structurte in all levels is same

<regPackagingHierarchyList>
  <RegistrationPackagingHierarchy>
    <recordId>Level0</recordId>
    <regParentPackagingHierarchy>
      <RegistrationPackagingHierarchy>
        <recordId>level5</recordId>
        <regParentPackagingHierarchy>
          <RegistrationPackagingHierarchy>
            <recordId>level4</recordId>
            <regParentPackagingHierarchy>
              <RegistrationPackagingHierarchy>
                <recordId>level3</recordId>
                <regParentPackagingHierarchy>
                  <RegistrationPackagingHierarchy>
                    <recordId>level2</recordId>
                    <regParentPackagingHierarchy>
                      <RegistrationPackagingHierarchy>
                        <recordId>level1</recordId>
                      </RegistrationPackagingHierarchy>
                    </regParentPackagingHierarchy>
                  </RegistrationPackagingHierarchy>
                </regParentPackagingHierarchy>
              </RegistrationPackagingHierarchy>
            </regParentPackagingHierarchy>
          </RegistrationPackagingHierarchy>
        </regParentPackagingHierarchy>
      </RegistrationPackagingHierarchy>
    </regParentPackagingHierarchy>
  </RegistrationPackagingHierarchy>
</regPackagingHierarchyList>

Expected Result should be in the follwing format Level1,Level2,Level3,level4,Level5

Community
  • 1
  • 1
  • Could you have two `RegistrationPackagingHierarchy` elements that were siblings of each other, or will there only ever be one `RegistrationPackagingHierarchy` child per parent? – Tim C Apr 04 '19 at 09:48
  • It will be always one child @TimC – DhanaSekhar Apr 04 '19 at 09:56

2 Answers2

3

One way in 1.0 can be:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" />

<xsl:template match="/">
    <xsl:for-each select="//regParentPackagingHierarchy//recordId">
        <xsl:sort select="position()" order="descending" />
        <xsl:value-of select="." />
        <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/pPzifpv

Edit:

<xsl:value-of select="concat('Count(regPackagingHierarchyList) = ', count(//regPackagingHierarchyList))"/>
<xsl:value-of select="concat('Count(RegistrationPackagingHierarchy) = ', count(//RegistrationPackagingHierarchy))"/>

Edit 2:

http://xsltfiddle.liberty-development.net/pPzifpv/8

Vebbie
  • 1,669
  • 2
  • 12
  • 18
  • Thank You so Much.. it's working as expected @Vebbie – DhanaSekhar Apr 04 '19 at 10:44
  • How to take child nodes count for or . expected output is **Count(regPackagingHierarchyList)=17** – DhanaSekhar Apr 04 '19 at 12:47
  • @DhanaSekhar : Added that in the answer. – Vebbie Apr 04 '19 at 12:56
  • @Vibbie, got another Problem. this is tag is repeating and while running your XSLT i am getting all Combined together.like **level1,level2,level3,level4,level5level1,level2,level3,level4,level5**. can you please Help me to fix this. Expected is in first Iteration i should get Iteration-1 ::level1,level2,level3,level4,level5 Iteration2:level1,level2,level3,level4,level5 ll be waiting for your reply. updated Here [link](http://xsltfiddle.liberty-development.net/pPzifpv/7) – DhanaSekhar Apr 04 '19 at 13:07
  • this is how XMl strcture will be ** Parent Package LIST Parent Package LIST Parent Package LIST ** – DhanaSekhar Apr 04 '19 at 13:20
0
<xsl:template match="*">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="recordId">
    <xsl:apply-templates/> 
    <!-- Apply all the templates beneath first and output your id after they got applied. -->
    <xsl:if test="ancestor::regParentPackagingHierarchy">
        <xsl:value-of select="."/>
    </xsl:if>
</xsl:template>
Christian Mosz
  • 543
  • 2
  • 12