Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is my xml file (Hospitals.xml)


XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hospitals>
    <hospital>
        <department>
            <clinic>
                <cid>31</cid>
                <clinicName>c7</clinicName>
                <status>0</status>
            </clinic>
            <clinic>
                <cid>32</cid>
                <clinicName>c2</clinicName>
                <status>0</status>
            </clinic>
            <depId>21</depId>
            <departmentName>card</departmentName>
        </department>
        <hospId>11</hospId>
        <hospitalName>aaa</hospitalName>
    </hospital>
    <hospital>
        <department>
            <clinic>
                <cid>33</cid>
                <clinicName>c3</clinicName>
                <status>0</status>
            </clinic>
            <clinic>
                <cid>34</cid>
                <clinicName>c4</clinicName>
                <status>0</status>
            </clinic>
            <depId>22</depId>
            <departmentName>eye</departmentName>
        </department>
        <department>
            <clinic>
                <cid>36</cid>
                <clinicName>c1</clinicName>
                <status>0</status>
            </clinic>
            <depId>24</depId>
            <departmentName>mental</departmentName>
        </department>
        <hospId>12</hospId>
        <hospitalName>bbbb</hospitalName>
    </hospital>
    <hospital>
        <department>
            <clinic>
                <cid>30</cid>
                <clinicName>c6</clinicName>
                <status>0</status>
            </clinic>
            <clinic>
                <cid>35</cid>
                <clinicName>c5</clinicName>
                <status>0</status>
            </clinic>
            <depId>23</depId>
            <departmentName>dental</departmentName>
        </department>
        <hospId>13</hospId>
        <hospitalName>cccc</hospitalName>
    </hospital>
    <hospital>
        <department>
            <clinic>
                <cid>37</cid>
                <clinicName>c8</clinicName>
                <status>0</status>
            </clinic>
            <depId>25</depId>
            <departmentName>test</departmentName>
        </department>
        <hospId>14</hospId>
        <hospitalName>ddd</hospitalName>
    </hospital>
</hospitals>




I need to change the value of status field when I based on the clinic I have selected

I tried like this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XML
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="hospId" />
<xsl:param name="depId" />
<xsl:param name="clinicId" />

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

<xsl:template match="hospitals/hospital[hospId='$hospId']/department[depId='$depId']/clinic[cid='$clinicId']/status">
     </status>123</status>
    </xsl:template>
</xsl:stylesheet>


its giving some error please help me..Thanks in advance
Posted
Updated 17-Mar-13 16:21pm
v4
Comments
barneyman 1-Aug-12 2:05am    
how far have you got?
Sandeep Mewara 1-Aug-12 2:15am    
And? Where are you stuck? What have you tried?

1 solution

there are several mistakes in your xsl stylesheet :
status is not properly xml formed and starts with an ending tag,
and lose the quotes you put around your $variables then it should work perfectly

I used following xsl stylesheet with saxon :

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

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="hospId" />
<xsl:param name="depId" />
<xsl:param name="clinicId" />

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

<xsl:template match="hospitals/hospital[hospId=$hospId]/department[depId=$depId]/clinic[cid=$clinicId]/status">
  <status>123</status>
</xsl:template>

</xsl:stylesheet>
 
Share this answer
 
v2
Comments
HCIMS DEVELOPER 2-Aug-12 6:37am    
@philip Stuyck: I tried the same but the xml data (ie status) is not changing do I need to add any extra code in xml????
Philip Stuyck 2-Aug-12 6:43am    
you need to call the xslt engine with the parameters correctly filled in otherwise indeed nothing changes. I did it like this :
C:\Users\stuyckp\Documents>"C:\Program Files\Saxonica\Saxonica\transform"
hospitals.xml hospitals.xsl hospId=14 depId=25 clinicId=37
And this gave me the correct result.
HCIMS DEVELOPER 2-Aug-12 6:55am    
@philip Stuyck: I didn't get you what you mean?...Where should I write the code
C:\Users\stuyckp\Documents>"C:\Program Files\Saxonica\Saxonica\transform"
hospitals.xml hospitals.xsl hospId=14 depId=25 clinicId=37

Do I need to install any software for the same
Philip Stuyck 2-Aug-12 10:13am    
I am using saxon, this is just an xslt engine, but you can use your own engine if you have one. But since you are using parameters, you have to specify those parameters and their values when you invoke the processor. I am doing that via the commandline arguments as I am showing you. If I don't pass in these commandline parameters I would get the same result back as the input xml file.
Since no change is occurring for you I think you are not passing the parameters to the processing engine you are using. Btw, which one is that ?
I am quite confident that my xsl stylesheet is correct.
HCIMS DEVELOPER 3-Aug-12 3:12am    
@Philip Stuyck: I had installed Saxon and gave the command as you gave and its working fine,but the thing the result is shown in command prompt only, not modifying or saving in the xml file. Is there any solution for the same

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