如何使用XSL修改XML的一小部分

Cha*_*ndu 0 xml xslt

我有一个包含25个元素的XML文件.我只想转换2个元素并保留剩余的XML.有人可以告诉我该怎么做.在线的所有例子都是整个修改xml doc,我不想要这个.我只想修改两个元素的值.

Mar*_*nen 8

通过使用身份转换模板来解决这些任务

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

然后通过添加要更改的元素的模板,例如

<xsl:template match="foo">
  <bar>
   <xsl:apply-templates select="@* | node()"/>
  </bar>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

foobar元素和/或元素的更改

<xsl:template match="foobar"/>
Run Code Online (Sandbox Code Playgroud)

删除foobar元素.

为了给你一个进一步的例子,例如,如果我们想要复制baz带有内容的元素但想要添加new元素,我们可以添加一个模板

<xsl:template match="baz">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <new>...</new>
  </xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

只要您将身份转换保持活动(使用apply-templates),您就可以通过为每个要更改的元素编写模板来很好地构建样式表.