使用基于参数的 XSLT 更新元素的文本 [英] Update the text of an element with XSLT based on param
问题描述
我正在尝试做一些看起来应该很简单的事情,但我无法让它工作,而且我似乎找不到任何不涉及很多不相关的事情的例子.我想将特定 xml 标记的文本内容更新为特定值(作为参数传入,此 XSLT 将从 ant 中使用).一个简单的例子:
我要变身
<酒吧>巴兹</bar></foo>
到
<酒吧>有些不同</bar></foo>
这是我试过的样式表,结果只有标签,根本没有文本
<!-- 身份转换,保持一切不变,除了我们想要改变的东西--><!-- 每当匹配任何节点或任何属性时 --><xsl:template match="node()|@*"><!-- 复制当前节点--><xsl:copy><!-- 包括它拥有的任何属性和任何子节点--><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><!-- 更改 bar 节点的文本,在真实模板中不会内联指定值 --><xsl:template match="/foo/bar/"><xsl:param name="baz" value="一些不同的东西"/><xsl:value-of select="$baz"/></xsl:模板></xsl:stylesheet>
提前致谢!
所提供的代码存在许多问题,导致编译时错误:
<块引用><xsl:param name="baz" value="一些不同的东西"/><xsl:value-of select="$baz"/></xsl:模板>
此模板上指定的匹配模式在语法上是非法的 -- XPath 表达式不能以
/
字符结尾.xsl:param
不能有未知属性,例如value
解决方案:
<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:param name="pReplacement" select="'Something Different'"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="foo/bar/text()"><xsl:value-of select="$pReplacement"/></xsl:模板></xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<酒吧>巴兹</bar></foo>
产生想要的、正确的结果:
<bar>不同的东西</bar></foo>
I'm trying to do something that seems like it should be very simple, but I can't get it working, and I can't seem to find any examples that don't involve lots of irrelevant things. I want to update the text content of a specific xml tag to a specific value (passed in as a parameter, this XSLT will be used from ant). A simple example :
I want to transform
<foo>
<bar>
baz
</bar>
</foo>
To
<foo>
<bar>
something different
</bar>
</foo>
This is the stylesheet that I tried, which results in just the tags, no text at all
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity transformation, to keep everything unchanged except for the stuff we want to change -->
<!-- Whenever you match any node or any attribute -->
<xsl:template match="node()|@*">
<!-- Copy the current node -->
<xsl:copy>
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- change the text of the bar node, in the real template the value won't be specified inline -->
<xsl:template match="/foo/bar/">
<xsl:param name="baz" value="something different"/>
<xsl:value-of select="$baz"/>
</xsl:template>
</xsl:stylesheet>
Thanks in advance!
There are a number of problems with the provided code which lead to compile-time errors:
<xsl:template match="/foo/bar/"> <xsl:param name="baz" value="something different"/> <xsl:value-of select="$baz"/> </xsl:template>
The match pattern specified on this template is syntactically illegal -- an XPath expression cannot end with the
/
character.xsl:param
cannot have an unknown attribute such asvalue
Solution:
<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="pReplacement" select="'Something Different'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo/bar/text()">
<xsl:value-of select="$pReplacement"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<foo>
<bar>
baz
</bar>
</foo>
the wanted, correct result is produced:
<foo>
<bar>Something Different</bar>
</foo>
这篇关于使用基于参数的 XSLT 更新元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!