Xslt 使用xpath获取节点的文本和元素节点子节点
我有一个包含标签和文本的标签Xslt 使用xpath获取节点的文本和元素节点子节点,xslt,xpath,Xslt,Xpath,我有一个包含标签和文本的标签 <p> Hello world <xref rid='1234'>1234</xref> this is a new world starting <xref rid="5678">5678</xref> finishing the new world </p> 你好,世界1234这是一个新世界的开始 5678 完成新世界 我将使用xslt对其进行转换,在输出中,我需要替换的,这是一
<p>
Hello world <xref rid='1234'>1234</xref> this is a new world starting
<xref rid="5678">5678</xref>
finishing the new world
</p>
你好,世界1234这是一个新世界的开始
5678
完成新世界
我将使用xslt对其进行转换,在输出中,我需要替换的
,这是一个新世界的开始
完成新世界
XSLT中处理这类事情的标准方法是使用标识模板逐字复制从输入到输出的所有内容,然后在需要更改某些内容时使用特定模板覆盖这些内容
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- identity template to copy everything as-is unless overridden -->
<xsl:template match="*@|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<!-- replace xref with a -->
<xsl:template match="xref">
<a><xsl:apply-templates select="@*|node()" /></a>
</xsl:template>
<!-- replace rid with href -->
<xsl:template match="xref/@rid">
<xsl:attribute name="href"><xsl:value-of select="." /></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
如果知道每个xref
元素肯定都有rid
属性,则可以将两个“特定”模板组合为一个
请注意,没有任何基于XSLT的解决方案能够保留这样一个事实:一些输入元素对属性使用单引号,而另一些使用双引号,因为这些信息在XPath数据模型中不可用(就XML解析器而言,这两种形式完全相同)。XSLT处理器很可能总是对其输出的所有元素使用一个或另一个,而不管输入元素是什么样子。解决方案非常简单(只有两个模板):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<a href="{@rid}"><xsl:apply-templates/></a>
</xsl:template>
</xsl:stylesheet>
<p>
Hello world <xref rid='1234'>1234</xref> this is a new world starting
<xref rid="5678">5678</xref>
finishing the new world
</p>
<p>
Hello world <a href="1234">1234</a> this is a new world starting
<a href="5678">5678</a>
finishing the new world
</p>
在提供的XML文档上应用此转换时:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<a href="{@rid}"><xsl:apply-templates/></a>
</xsl:template>
</xsl:stylesheet>
<p>
Hello world <xref rid='1234'>1234</xref> this is a new world starting
<xref rid="5678">5678</xref>
finishing the new world
</p>
<p>
Hello world <a href="1234">1234</a> this is a new world starting
<a href="5678">5678</a>
finishing the new world
</p>
你好,世界1234这是一个新世界的开始
5678
完成新世界
生成所需的正确结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<a href="{@rid}"><xsl:apply-templates/></a>
</xsl:template>
</xsl:stylesheet>
<p>
Hello world <xref rid='1234'>1234</xref> this is a new world starting
<xref rid="5678">5678</xref>
finishing the new world
</p>
<p>
Hello world <a href="1234">1234</a> this is a new world starting
<a href="5678">5678</a>
finishing the new world
</p>
你好,世界,这是一个新的世界
完成新世界
说明:
xsl:Attribute