Dhanasekhar Naidu
Dhanasekhar Naidu

Reputation: 17

How to count the all child nodes of parent inside for Loop in XSLT

need to count the number of childs and Grand Childs of Parent Node in all child Levels for the following XML.

How to find, number of childs under <regPackagingHierarchyList> with node as <recordId>

I tried following Code

<xsl:for-each select="LSRIMSData/agl_result/RegistrationPackaging/regPackagingHierarchyList/RegistrationPackagingHierarchy[recordId/text()!='']">
    <xsl:value-of select="count(child::regParentPackagingHierarchy/RegistrationPackagingHierarchy/recordId/node())"></xsl:value-of>
</xsl:for-each>

Source XML:

<agConnectXML>
  <SourceData>
    <SKUIDOut noNamespaceSchemaLocation="file:///c:/Users/BODDUAV1/OneDrive%20-%20Novartis%20Pharma%20AG/Avanthi/NovaRIM/Documents/SHAPE/stockKeepingUnit.xsd" schemaVersion="1.0">
      <SystemMessageHeader>
        <CreationDateTime>2002-10-10T12:00:00-05:00</CreationDateTime>
        <SenderID>sandoz</SenderID>
        <BusinessSystemID>SHAPE-P34-SKU</BusinessSystemID>
        <MessageID>678678-2389789-4893947-473946</MessageID>
      </SystemMessageHeader>
      <stockKeepingUnit>40</stockKeepingUnit>
      <stockKeepingUnitStatus>Approved</stockKeepingUnitStatus>
      <nationalTradeItemNumber>098098</nationalTradeItemNumber>
      <registrationId>REG-00000026</registrationId>
      <finishedDosageFormId>FDF-002</finishedDosageFormId>
      <activeSusbstanceId>6437</activeSusbstanceId>
      <tenant>sandoz</tenant>
    </SKUIDOut>
  </SourceData>
  <LSRIMSData>
    <agl_result>
      <agl_service_headers>
        <serviceId>CustgetRegPackDetails</serviceId>
        <messageProducer>agidmp</messageProducer>
        <internalVersion>12077</internalVersion>
        <uuid>5670bf06-4f0a-4f0f-ab93-2d2ad99a7384</uuid>
        <dateFormat>yyyy-MM-dd H:mm:ss</dateFormat>
        <generatedTimeStamp>2019-04-04 20:02:09</generatedTimeStamp>
        <user>system</user>
      </agl_service_headers>
      <agl_pagination_details>
        <start>0</start>
        <limit>10</limit>
        <totalRecordsCount>1</totalRecordsCount>
      </agl_pagination_details>
      <RegistrationPackaging>
        <productPackaging>
          <ProductPackaging>
            <packagingItemName>FDF-002</packagingItemName>
          </ProductPackaging>
        </productPackaging>
        <regIncludedProduct>
          <RegistrationIncludedProduct>
            <recordId>43893</recordId>
            <registration>
              <Registration>
                <dataState>C</dataState>
                <recordId>43887</recordId>
                <registrationUID>REG-00000026</registrationUID>
              </Registration>
            </registration>
          </RegistrationIncludedProduct>
        </regIncludedProduct>
        <regPackagingHierarchyList>
          <RegistrationPackagingHierarchy>
            <recordId>43914</recordId>
            <regParentPackagingHierarchy>
              <RegistrationPackagingHierarchy>
                <recordId>43912</recordId>
              </RegistrationPackagingHierarchy>
            </regParentPackagingHierarchy>
            <regPkgHierarchyDataCarrierList></regPkgHierarchyDataCarrierList>
            <regErpCodesList></regErpCodesList>
          </RegistrationPackagingHierarchy>
          <RegistrationPackagingHierarchy>
            <recordId>43916</recordId>
            <regParentPackagingHierarchy>
              <RegistrationPackagingHierarchy>
                <recordId>43914</recordId>
                <regParentPackagingHierarchy>
                  <RegistrationPackagingHierarchy>
                    <recordId>43912</recordId>
                    <regParentPackagingHierarchy>
                      <RegistrationPackagingHierarchy>
                        <recordId>43916</recordId>
                      </RegistrationPackagingHierarchy>
                    </regParentPackagingHierarchy>
                  </RegistrationPackagingHierarchy>
                </regParentPackagingHierarchy>
              </RegistrationPackagingHierarchy>
            </regParentPackagingHierarchy>
            <regPkgHierarchyDataCarrierList></regPkgHierarchyDataCarrierList>
            <regErpCodesList></regErpCodesList>
          </RegistrationPackagingHierarchy>
        </regPackagingHierarchyList>
      </RegistrationPackaging>
    </agl_result>
  </LSRIMSData>
</agConnectXML>

Expected result will be in the following format

Count(regParentPackagingHierarchy/RegistrationPackagingHierarchy/RecordIds) = 1 Count(regParentPackagingHierarchy/RegistrationPackagingHierarchy/RecordIds) = 3

Upvotes: 0

Views: 527

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117140

How to find, number of childs under <regPackagingHierarchyList> with node as <recordId>

There is only one instance of regPackagingHierarchyList in your input. Assuming you meant RegistrationPackagingHierarchy, and that you actually want to count all recordId elements that are descendants (not only children) of each RegistrationPackagingHierarchy, you could try something like:

<xsl:template match="/agConnectXML">
    <xsl:for-each select="LSRIMSData/agl_result/RegistrationPackaging/regPackagingHierarchyList/RegistrationPackagingHierarchy">
        <xsl:text>count = </xsl:text>
        <xsl:value-of select="count(descendant::recordId)"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

Given your XML example, the result will be:

count = 2
count = 4

Upvotes: 0

Related Questions