XSLT:复制节点并修改它们 [英] XSLT: copy Nodes and modify them
本文介绍了XSLT:复制节点并修改它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入:
<xsl:variable name="nodelist">
<root>
<a size="12" number="11">
<sex>male</sex>
Jens
</a>
<a size="12" number="11">
<sex>male</sex>
Hulk
</a>
<a size="12" number="11">
<sex>male</sex>
Steven XXXXXXXXXXX
</a>
<a size="12" number="11">
<sex>male</sex>
Joschua
</a>
<a size="12" number="11">
<sex>female</sex>
Angelina
</a>
</root>
</variable>
所需的输出:
<root>
<a size="12" number="11">
<sex>male</sex>
Jens
</a>
<a size="12" number="11">
<sex>male</sex>
Hulk
</a>
<a size="12" number="11">
<sex>male</sex>
Steven YYYYYYYYYYYY
</a>
<a size="12" number="11">
<sex>male</sex>
Joschua
</a>
<a size="12" number="11">
<sex>female</sex>
Angelina
</a>
</root>
我想用 XXXXXXXXXXX 更改 a 节点.我可以复制第一个和最后两个节点,更改第三个,然后像这样重新组合在一起.(XLST 1.0)
I want to change the a node with XXXXXXXXXXX. Can I copy the first and last two nodes, change the third and then put back together again like this. (XLST 1.0)
<xsl:variable name="begin">
<xsl:value-of select="substring-before($nodelist, 'XXXXXXXXXXX')"/>
</xsl:variable>
<xsl:variable name="replaceString">
YYYYYYYYYYYY
</xsl:variable>
<xsl:variable name="end">
<xsl:value-of select="substring-after($nodelist, 'xxxxx')"/>
</xsl:variable>
<xsl:variable name="all">
<xsl:copy-of select="$begin"/>
<xsl:copy-of select="$replaceString"/>
<xsl:copy-of select="$end"/>
</xsl:variable>
使用子字符串,我丢失了有关节点的所有信息.这是带有子串的结果
With substring i have lost all information about the nodes. This is the result with substring
<root>
male Jens
male Hulk
male Steven YYYYYYYYYYYY
male Joschua
female Angelina
</root>
推荐答案
你需要让你的样式表更有针对性.仅更改包含需要替换的值的 text()
.对于其他一切,身份模板将确保复制内容:
You need to make your stylesheet a bit more targeted. Changing only the text()
that contains the value that needs to be replaced. For everything else, the identity template will ensure that the content is copied:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--Identity template will copy all matched nodes and apply-templates-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Specialized template to match on text nodes that contain the "findString" value-->
<xsl:template match="text()[contains(.,'XXXXXXXXXXX')]">
<xsl:variable name="findString" select="'XXXXXXXXXXX'"/>
<xsl:variable name="replaceString" select="'YYYYYYYYYYYY'"/>
<xsl:value-of select="concat(substring-before(., $findString),
$replaceString,
substring-after(., $findString))"/>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT:复制节点并修改它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文