5

我需要通过 xsl 文件更改 xml 中节点的特定元素的值下面是我的 xml 数据

<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>

例如,我需要根据其 id 即 9 选择 Clinic9 并将状态 0 更改为 1

我试过这样

<?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>

但价值并没有改变......

4

2 回答 2

6

如果您尝试修改/替换特定元素,则需要匹配该元素。例如,如果您尝试替换特定status元素,则需要匹配该特定元素。

修改后的 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 输出

<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>
于 2012-08-02T07:13:29.333 回答
0

听起来好像您正在尝试使用 XSLT 来更改原始 XML 文件。XSLT 不能更改原始文件。您可以使用它来转换 XML 并通过使用运行 XSLT 的程序或脚本将其输出到另一个 XML。

于 2012-08-02T06:49:56.093 回答