Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XSLT: Add few Variables to List

I am trying to Add a variable to a list from XML using XSTL and create the merged tag but it is not happening.

Source XML.

<Result> 
        <Data>
        <Pass>true</Pass>
        <Data> 
                <account> 
                        <accountNumber>1111</accountNumber> 
                </account> 
                <account> 
                          <accountNumber>2222</accountNumber> 
                </account> 
</Result> 

Requirement is that the output tag should have the list with Pass tag value like below.

<Student>
   <accountNumber>1111</accountNumber> 
   <Pass>true</Pass> 
<Student>  
<Student>
   <accountNumber>2222</accountNumber> 
   <Pass>true</Pass> 
<Student>

Current XSLT file is giving the list but I am unable to add Pass Tag.

<xsl:template match = "Root/Result">
        <xsl:apply-templates select="account"/>
</xsl:template>

<xsl:template match = "account">
  <Student>
    <accountNumber><xsl:value-of select="accountNumber" /></accountNumber>
  </Student> 
</xsl:template>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

  <xsl:template match = "account">
    <Student>
      <accountNumber><xsl:value-of select="accountNumber" /></accountNumber>
      <xsl:copy-of select="preceding-sibling::Pass"/>
    </Student> 
  </xsl:template>

Update. Since the source data changed this is what you then need:

      <xsl:template match = "account">
        <Student>
          <accountNumber><xsl:value-of select="accountNumber" /></accountNumber>
          <xsl:copy-of select="preceding-sibling::Data/Pass"/>
        </Student> 
      </xsl:template>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading