Click here to Skip to main content
15,891,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,
I have an xml out of which i need to create another one base on input passed, which in this case is claim number. This output is not just a part of XML but the tree structure needs to be maintained as a whole base leaves value.

Following is the main xml :
XML
<claimdata>
  <payer payerid="p1">
    <plan planno="pl1">
      <subscriber subscriberid="s1">
        <patient patientid="pt1">
          <claim claimno="CL1" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
          <claim claimno="CL2" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
          <claim claimno="CL3" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
        <patient patientid="pt2">
          <claim claimno="CL4" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
          <claim claimno="CL5" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
        <patient patientid="pt3">
          <claim claimno="CL6" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
      <subscriber subscriberid="s2">
        <patient patientid="pt4">
          <claim claimno="CL7" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
    </plan>
    <plan planno="pl2">
      <subscriber subscriberid="s3">
        <patient patientid="pt5">
          <claim claimno="CL8" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
    </plan>
  </payer>
  <payer payerid="p2">
    <plan planno="pl3">
      <subscriber subscriberid="s4">
        <patient patientid="pt6">
          <claim claimno="CL9" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
        <patient patientid="pt7">
          <claim claimno="CL10" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
          <claim claimno="CL11" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
    </plan>
  </payer>
</claimdata>


And the desired output is below based on the input of claim numbers (CL2,CL5 and CL7):
XML
<claimdata>
  <payer payerid="p1">
    <plan planno="pl1">
      <subscriber subscriberid="s1">
        <patient patientid="pt1">
          <claim claimno="CL2" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
        <patient patientid="pt2">
          <claim claimno="CL5" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
      <subscriber subscriberid="s2">
        <patient patientid="pt4">
          <claim claimno="CL7" amount="10">
            <claimline lineno="l1" lineamount="5"></claimline>
            <claimline lineno="l2" lineamount="5"></claimline>
          </claim>
        </patient>
      </subscriber>
    </plan>
  </payer>
</claimdata>


Thanks In Advance.
Posted
Updated 20-Jan-13 8:41am
v3
Comments
Sandeep Mewara 19-Jan-13 15:40pm    
And the issue is?
Hitendra Tiwari 19-Jan-13 22:22pm    
I can't figure out how to do it. For eg if I run XPath //claim[@claimno="CL5"] i get below result:
<claim claimno="CL5" amount="10">
<claimline lineno="l1" lineamount="5">
<claimline lineno="l2" lineamount="5">
</claimline></claimline></claim>

But what i want is entire tree with other claims and their parents missing i.e.:
<claimdata>
<payer payerid="p1">
<plan planno="pl1">
<subscriber subscriberid="s1">
<patient patientid="pt2">
<claim claimno="CL5" amount="10">
<claimline lineno="l1" lineamount="5"></claimline>
<claimline lineno="l2" lineamount="5"></claimline>
</claim>
</patient>
</subscriber>
</plan>
</payer>
</claimdata>

and result should be as i have given in above comment based on multiple claimno's being passed to my function.

1 solution

Maybe the following is a starting point (for one record):
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="claim">
    <xsl:choose>
      <xsl:when test="@claimno='CL5'">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise/>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Cheers
Andi
 
Share this answer
 
Comments
Hitendra Tiwari 21-Jan-13 2:12am    
Hi Andi,
I tried your code above and gives incorrect result. It provides that particular claim but also gives every other patient, subscriber and plan (which are empty and do not contain any claim). It is as following:
<?xml version="1.0" encoding="utf-8"?>
<claimdata>
<payer payerid="p1">
<plan planno="pl1">
<subscriber subscriberid="s1">
<patient patientid="pt1">



</patient>
<patient patientid="pt2">

<claim claimno="CL5" amount="10">
<claimline lineno="l1" lineamount="5" />
<claimline lineno="l2" lineamount="5" />
</claim>
</patient>
<patient patientid="pt3">

</patient>
</subscriber>
<subscriber subscriberid="s2">
<patient patientid="pt4">

</patient>
</subscriber>
</plan>
<plan planno="pl2">
<subscriber subscriberid="s3">
<patient patientid="pt5">

</patient>
</subscriber>
</plan>
</payer>
<payer payerid="p2">
<plan planno="pl3">
<subscriber subscriberid="s4">
<patient patientid="pt6">

</patient>
<patient patientid="pt7">


</patient>
</subscriber>
</plan>
</payer>
</claimdata>
Andreas Gieriet 21-Jan-13 2:49am    
Are you familiar with XSLT or do you expect a ready to use solution where you do not have to do anything yourself?
Show me what you tried to do.
This is not a gimme-code-no-effort forum.
Cheers
Andi
Hitendra Tiwari 21-Jan-13 7:27am    
I'm sorry about that, i'm really not familiar with XSLT. I can understand your feeling, but all i needed was a general guidance if such kind of solutions are achievable with XSLT before i resort to string manipulation with c#.

Thanks
Andreas Gieriet 21-Jan-13 7:44am    
You can do it but you need to understand XSLT. Not a beginners task. XSLT is a "functional language", i.e. you have no concept of *variables*. I.e. there are variables, but they can only be initialized, not modified.
If you just start, go and work through some XSLT Tutorial, like the one from w3schools.com.
Good luck!
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900