简体   繁体   中英

using xslt how to replace the node

<h:body>
<group id = "1">
<name>xxx</name>
<age>12</age>
<group id = "2">
<name>yyy</name>
<age>13</age>
</h:body>

using XSLT i want to replace id=1 with <group appearance = "field-list">

Use the identity transform , overriding the part you wish to replace with something different. To wit:

Given this input XML document:

<h:body xmlns:h="http://example.org/h">
  <group id = "1">
    <name>xxx</name>
    <age>12</age>
  </group>
  <group id = "2">
    <name>yyy</name>
    <age>13</age>
  </group>
</h:body>

This XSLT transformation:

<xsl:stylesheet version="1.0"
                xmlns:h="http://example.org/h"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="group[@id = '1']">
    <group appearance = "field-list">
      <xsl:apply-templates select="node()|@*"/>
    </group>
  </xsl:template>
</xsl:stylesheet>

Will produce this output XML document:

<h:body xmlns:h="http://example.org/h">
  <group appearance="field-list" id="1">
      <name>xxx</name>
      <age>12</age>
  </group>
  <group id="2">
      <name>yyy</name>
      <age>13</age>
  </group>
</h:body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM