6

I need to change the value of particular element of a node in a xml through xsl file Below is my xml data

<hospitals>
  <hospital>
    <department>
      <clinic>
        <cid>8</cid>
        <clinicName>clinic8</clinicName>
        <status>1</status>
      </clinic>
      <clinic>
        <cid>9</cid>
        <clinicName>clinic9</clinicName>
        <status>0</status>
      </clinic>
      <depId>3</depId>
      <departmentName>dental</departmentName>
    </department>
    <hospId>2</hospId>
    <hospitalName>appolo</hospitalName>
  </hospital>
  <hospital>
    <department>
      <clinic>
        <cid>82</cid>
        <clinicName>clinic82</clinicName>
        <status>0</status>
      </clinic>
      <clinic>
        <cid>92</cid>
        <clinicName>clinic92</clinicName>
        <status>0</status>
      </clinic>
      <depId>4</depId>
      <departmentName>mental</departmentName>
    </department>
    <hospId>2</hospId>
    <hospitalName>manipal</hospitalName>
  </hospital>
</hospitals>

for example, I need to select clinic9 based on its id ie 9 and change the status 0 to 1

I tried like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="hospId"/>
<xsl:param name="depId" />
<xsl:param name="clinicId"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="hospitals/hospital[hospId='2']/department[depId='3']/clinic[cid='9']">
<xsl:choose>
 <xsl:when test="cid ='9'">
  <xsl:element name="status">123</xsl:element>
  </xsl:when>
</xsl:choose>
</xsl:template>
  </xsl:stylesheet>

But the value is not changing...

alk
  • 69,737
  • 10
  • 105
  • 255
subbusaabu
  • 181
  • 3
  • 3
  • 12

2 Answers2

7

If you are trying to modify/replace a specific element, you need to match that element. For example, if you are trying to replace a specific status element, you need to match that specific element.

Modified XSLT

<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" select="'2'"/>
    <xsl:param name="depId" select="'3'"/>
    <xsl:param name="clinicId" select="'9'"/>

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

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

XML Output

<hospitals>
   <hospital>
      <department>
         <clinic>
            <cid>8</cid>
            <clinicName>clinic8</clinicName>
            <status>1</status>
         </clinic>
         <clinic>
            <cid>9</cid>
            <clinicName>clinic9</clinicName>
            <status>123</status>
         </clinic>
         <depId>3</depId>
         <departmentName>dental</departmentName>
      </department>
      <hospId>2</hospId>
      <hospitalName>appolo</hospitalName>
   </hospital>
   <hospital>
      <department>
         <clinic>
            <cid>82</cid>
            <clinicName>clinic82</clinicName>
            <status>0</status>
         </clinic>
         <clinic>
            <cid>92</cid>
            <clinicName>clinic92</clinicName>
            <status>0</status>
         </clinic>
         <depId>4</depId>
         <departmentName>mental</departmentName>
      </department>
      <hospId>2</hospId>
      <hospitalName>manipal</hospitalName>
   </hospital>
</hospitals>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
0

It sounds as if you are trying to use XSLT to change the original XML file. XSLT cannot change the original file. You may use it to transform the XML and output it to another XML through the use of a program or script that runs XSLT.

magix
  • 224
  • 4
  • 12
  • So what I need to use when I need to modify particular element in a node in xml – subbusaabu Aug 02 '12 at 06:56
  • You can a. manually replace the XML file with the XSLT output, b. use XForms (a technique for building editing Forms that is capable of changing XML files) or c. use a language that is able to trigger XSLT transformations, receive the result and save it as a file (e.g. PHP, see this answer: http://stackoverflow.com/questions/4946833/xml-outputting-php-vs-js-vs-anything-else/4948312#4948312 ) – Andreas Aug 02 '12 at 07:27