【发布时间】:2017-08-02 15:50:18
【问题描述】:
我想转换一条我知道是有效 xml 的评论。比如下面我想把bar属性的值从x改成y。
<root><!-- <foo bar="x"/> --></root>
我创建的样式表如下
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="comment()">
<xsl:comment>
<xsl:variable name="xml-string">
<xsl:value-of select="current()" />
</xsl:variable>
<xsl:variable name="xml-node" select="exslt:node-set($xml-string)" />
<xsl:apply-templates select="$xml-node" />
</xsl:comment>
</xsl:template>
<xsl:template match="@bar[parent::foo]">
<xsl:attribute name="bar">
<xsl:value-of select="'y'" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
但我从中得到的输出是
<?xml version="1.0" encoding="UTF-8"?><root><!-- <foo bar="x"/> --></root>
我怀疑节点集函数实际上并没有像我想象的那样做。
非常感谢任何帮助。
【问题讨论】:
-
您需要使用 XSLT 3.0 和
parse-xml开始,然后您还需要解释结果是否应该包含foo元素节点或再次包含注释。在后一种情况下,您还需要 XSLT 3.0 中的serialize函数。