Jump to content
Sign in to follow this  
nagabhb@gmail.com

XSLT multiple elements with the same name, same level

Recommended Posts

Hi , Need help in xslt logic.

 

I've to read the 'HierCd' element value from the below input xml and

 

if the value is 003 then create a tag like <MCA> 000000001003782 </MCA> // fetch the value of corresponding CntlAcctNbr

if the value is 004 then create a tag like <BCA> 378571802991005 <BCA> // fetch the value of corresponding CntlAcctNbr

If the value is 005 then create a tag like <LCA>378571802991006</LCA> // fetch the value of corresponding CntlAcctNbr

 

Please note that there could be multiple elements exist with same 'HierCd' , however need to read the first entry and discard remaining.

 

 

Input XML:

 

<CardAcctHierarchyRln>
<CorpId>702653</CorpId>
<CorpNm></CorpNm>
<CardAcctHierarchy>
<CntlAcctNbr>000000001003782</CntlAcctNbr>
<CntlAcctNm></CntlAcctNm>
<HierCd>003</HierCd>
</CardAcctHierarchy>
<CardAcctHierarchy>
<CntlAcctNbr>378571802991005</CntlAcctNbr>
<CntlAcctNm></CntlAcctNm>
<HierCd>004</HierCd>
</CardAcctHierarchy>
<CardAcctHierarchy>
<CntlAcctNbr>378571802991006</CntlAcctNbr>
<CntlAcctNm></CntlAcctNm>
<HierCd>005</HierCd>
</CardAcctHierarchy>
<CardAcctHierarchy>
<CntlAcctNbr>378571802991004</CntlAcctNbr>
<CntlAcctNm></CntlAcctNm>
<HierCd>004</HierCd>
</CardAcctHierarchy>
<CardAcctHierarchy>
<CntlAcctNbr>378571802991004</CntlAcctNbr>
<CntlAcctNm></CntlAcctNm>
<HierCd>003</HierCd>
</CardAcctHierarchy>

</CardAcctHierarchyRln>

OutPut:

 

<MCA> 000000001003782 </MCA>

<BCA> 378571802991005 <BCA>

<LCA>378571802991006</LCA>

 

Appreciate your help on this.

 

Thanks

Nag

Share this post


Link to post
Share on other sites

MOst of us don't use XML based technologies, but I think you can use the <xsl:choose> to determine which output element to use for this. Something like this:

<xsl:choose>
  <xsl:when test="HierCD='003'">
    <MCA><xsl:value-of select="CntlAcctNbr" /></MCA>
  </xsl:when>
  <xsl:otherwise>
    <xsl:when test="HierCD='004'">
      <BCA><xsl:value-of select="CntlAcctNbr" /></BCA>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="HierCD='005'">
        <LCA><xsl:value-of select="CntlAcctNbr" /></LCA>
      </xsl:if>
    </xsl:otherwise>
  </xsl:otherwise>
</xsl:choose> 

I haven't used XSL in years, so I might have gotten the syntax wrong, but the concept should be clear.

 

Since XSL is not a complete programming language and does not have variables, I don't think there is any way to keep track of which HierCD values have already been used. You probably need to use a real programming language and a DOM library to process the XML in that case.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×