Xml 替换节的XSLT
我试图用XSLT替换XML中的一个部分 输入:Xml 替换节的XSLT,xml,xslt,Xml,Xslt,我试图用XSLT替换XML中的一个部分 输入: <data> <entry> <id>1</id> <propertyA>10</propertyA> <propertyB>20</propertyB> </entry> <entry> <id>2</id> &
<data>
<entry>
<id>1</id>
<propertyA>10</propertyA>
<propertyB>20</propertyB>
</entry>
<entry>
<id>2</id>
<propertyA>8</propertyA>
<propertyB>12</propertyB>
</entry>
</data>
<data>
<entry>
<id>1</id>
<propertyA>15</propertyA>
<propertyB>8</propertyB>
</entry>
<entry>
<id>2</id>
<propertyA>8</propertyA>
<propertyB>12</propertyB>
</entry>
</data>
输出中缺少
条目
元素,因为您没有将其复制到输出中。此外,在满足“何时”条件的情况下:
<xsl:when test="id=$replaceId"></xsl:when>
输出
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="replaceID" select="'2'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="propertyA[parent::entry/id = $replaceID]">
<xsl:copy>
<xsl:value-of select="15"/>
</xsl:copy>
</xsl:template>
<xsl:template match="propertyB[parent::entry/id = $replaceID]">
<xsl:copy>
<xsl:value-of select="8"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<entry>
<id>1</id>
<propertyA>15</propertyA>
<propertyB>8</propertyB>
</entry>
<entry>
<id>2</id>
<propertyA>8</propertyA>
<propertyB>12</propertyB>
</entry>
</data>
1.
15
8.
2.
8.
12
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="replaceID" select="'2'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="propertyA[parent::entry/id = $replaceID]">
<xsl:copy>
<xsl:value-of select="15"/>
</xsl:copy>
</xsl:template>
<xsl:template match="propertyB[parent::entry/id = $replaceID]">
<xsl:copy>
<xsl:value-of select="8"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<data>
<entry>
<id>1</id>
<propertyA>15</propertyA>
<propertyB>8</propertyB>
</entry>
<entry>
<id>2</id>
<propertyA>8</propertyA>
<propertyB>12</propertyB>
</entry>
</data>