如何从标记存储在变量中的树中选择节点? [英] How can I select nodes from a tree whose markup is stored in a variable?
问题描述
考虑以下 XSLT 脚本:
Consider the following XSLT script:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:variable name="stringmap">
<map>
<entry><key>red</key><value>rot</value></entry>
<entry><key>green</key><value>gruen</value></entry>
<entry><key>blue</key><value>blau</value></entry>
</map>
</xsl:variable>
<xsl:template match="/">
<!-- IMPLEMENT ME -->
</xsl:template>
</xsl:stylesheet>
我希望这个脚本打印redgreenblue
.
有什么方法可以将存储在 stringmap
变量中的 XML 标记视为自己的文档,我可以在其上运行 XPath 查询?我基本上是在寻找类似的东西
Is there any way to treat the XML markup which is stored in the stringmap
variable as a document of its own which I can run XPath queries on? I'm basically looking for something like
<xsl:for-each select="document($stringmap)/map/entry">
<xsl:value-of select="key"/>
</xsl:for-each>
(除了 document()
函数需要一个 URI).
(except that the document()
function expects an URI).
动机:我有各种长
元素将给定的字符串映射到另一个字符串.我想用一个带有map"参数(这是一个简单的 XML 文档)的模板替换所有这些.我希望然后我可以用一个简单的语句替换 <xsl:choose>
像 <xsl:value-of select="$stringmap/map/entry/value[../key='$givenkey']"/>
Motivation: I have various long <xsl:choose>
elements which map a given string to another string. I'd like to replace all those with a single template which takes a 'map' argument (which is a simple XML document). My hope is that I can then replace the <xsl:choose>
with a simple statement like <xsl:value-of select="$stringmap/map/entry/value[../key='$givenkey']"/>
我通过 xsltproc 使用 XSLT 1.0.
I'm using XSLT 1.0 using xsltproc.
推荐答案
你几乎是对的,使用 document('')
将允许你处理当前样式表中的节点集:>
You're almost right, using document('')
will allow you to process node sets inside the current stylesheet:
<xsl:for-each select="document('')/xsl:stylesheet/xsl:variable[@name='stringmap']/map/entry">
<xsl:value-of select="key"/>
</xsl:for-each>
在这种情况下,不必将 map
节点集定义为变量:
It's not necessary to define the map
node set as a variable in this case:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:data="some.uri" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<data:map>
<entry><key>red</key><value>rot</value></entry>
<entry><key>green</key><value>gruen</value></entry>
<entry><key>blue</key><value>blau</value></entry>
</data:map>
<xsl:template match="/">
<xsl:for-each select="document('')/xsl:stylesheet/data:map/entry">
<xsl:value-of select="key"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果您不使用 xsl:variable
作为包装器,您必须记住顶级元素必须具有非空的命名空间 URI.
If you do not use xsl:variable
as a wrapper, you must remember that a top level elements must have a non null namespace URI.
在 XSLT 2.0 中,可以只迭代变量中的内容:
In XSLT 2.0 it would've been possible to just iterate over the content in a variable:
<xsl:variable name="map">
<entry><key>red</key><value>rot</value></entry>
<entry><key>green</key><value>gruen</value></entry>
<entry><key>blue</key><value>blau</value></entry>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="$map/entry">
<xsl:value-of select="key"/>
</xsl:for-each>
</xsl:template>
这篇关于如何从标记存储在变量中的树中选择节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!