Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML XSLT XSL
This is my xml file (Hospitals.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: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 31-Jul-12 19:35pm
Edited 17-Mar-13 16:21pm
RedDk9.4K
v4
Comments
barneyman at 1-Aug-12 2:05am
   
how far have you got?
Sandeep Mewara at 1-Aug-12 2:15am
   
And? Where are you stuck? What have you tried?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 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>
  Permalink  
v2
Comments
riyasn at 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 at 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.
riyasn at 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 at 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 at 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
Philip Stuyck at 3-Aug-12 3:26am
   
just use a pipe to capture the result by appending >output.xml
to the command.
If it works please mark this as a solution.
HCIMS DEVELOPER at 3-Aug-12 5:00am
   
@Philip: yes man its working...How can I make the thing as dynamic. Now we are executing through command, the same thing can't I run through xsl or using javascript
Philip Stuyck at 3-Aug-12 5:37am
   
there are tons of solutions to do this programatically.
Never done it in javascript myself but google is your friend and came up with this : http://www.w3schools.com/xsl/xsl_client.asp
I mainly did some xslt tranformations in .NET, there is a dedicated api for this in .NET.
Since this is working please rate this solution to give me due credit ;-)
HCIMS DEVELOPER at 3-Aug-12 6:23am
   
i am working in java and i need xslt server side scripting not the client side
Philip Stuyck at 3-Aug-12 6:25am
   
there are java xslt engine freely available too.

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

  Print Answers RSS
0 OriginalGriff 432
1 Kornfeld Eliyahu Peter 420
2 Yogesh Kumar Tyagi 244
3 Nelek 115
4 Gihan Liyanage 99
0 Kornfeld Eliyahu Peter 420
1 OriginalGriff 303
2 Yogesh Kumar Tyagi 230
3 Gihan Liyanage 99
4 sankarsan parida 90


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 17 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100