XSLT:在XSLT中创建一个地图 [英] XSLT : Creating a Map in XSLT
问题描述
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/ 1999 / XSL /变换>
< xsl:template match =/>
< xsl:variable name =map>
< map>
< entry key =key-1> value1< / entry>
< entry key =key-2> value2< / entry>
< entry key =key-3> value3< / entry>
< / map>
< / xsl:variable>
< output>
< xsl:value-of select =$ map / entry [@ key ='key-1']/>
< / output>
< / xsl:template>
< / xsl:stylesheet>
XSLT 2.0
使用XSLT 2.0,以下解决方案的工作原理是:
< xsl:variable name =map> ;
< entry key =key-1> value1< / entry>
< entry key =key-2> value2< / entry>
< entry key =key-3> value3< / entry>
< / xsl:variable>
< xsl:template match =/>
< output>
< xsl:value-of select =$ map / entry [@ key ='key-1']/>
< / output>
< / xsl:template>
XSLT 1.0
您不能使用XSLT 1.0中XPath表达式中的结果树片段,但 fn:document()
可以检索地图值。一个类似的问题将在这里工作:。
< xsl:value-of select = 文件( '')//的xsl:变量[@名称= '映射'] /图/条目[@键= '键-1']/>
如 XSLT 1.0规范:
)
是指
的根节点的样式表;样式表的树
表示是
完全相同,如果包含样式表的XML
文档是
的初始源文档。
< blockquote>
但是,您不需要为此使用
xsl:variable
。您可以直接在xsl:stylesheet
下指定地图节点,但您必须记住,顶级元素必须具有非空名称空间URI:< xsl:stylesheet
version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform
xmlns:my =some.uriexclude-result-prefixes =my>
< my:map>
< entry key =key-1> value1< / entry>
< entry key =key-2> value2< / entry>
< entry key =key-3> value3< / entry>
< / my:map>
< xsl:template match =/>
< output>
< xsl:value-of select =document('')/ * / my:map / entry [@ key ='key-1']/>
< / output>
< / xsl:template>
< / xsl:stylesheet>
I want to have a key value map in xsl and so defined a variable which has an xml fragment, but later when I try to access the xml nodes in the variable I get an error that type of the xpath xpression cannot be resolved.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="map"> <map> <entry key="key-1">value1</entry> <entry key="key-2">value2</entry> <entry key="key-3">value3</entry> </map> </xsl:variable> <output> <xsl:value-of select="$map/entry[@key='key-1']"/> </output> </xsl:template> </xsl:stylesheet>
解决方案XSLT 2.0
Using XSLT 2.0, the following solution works:
<xsl:variable name="map"> <entry key="key-1">value1</entry> <entry key="key-2">value2</entry> <entry key="key-3">value3</entry> </xsl:variable> <xsl:template match="/"> <output> <xsl:value-of select="$map/entry[@key='key-1']"/> </output> </xsl:template>
XSLT 1.0
You cannot use a result tree fragment in a XPath expression in XSLT 1.0, but
fn:document()
can retrieve map values. An answer to a similar question will work here:.<xsl:value-of select="document('')//xsl:variable[@name='map']/map/entry[@key='key-1']"/>
As described in the XSLT 1.0 specification:
document("")
refers to the root node of the stylesheet; the tree representation of the stylesheet is exactly the same as if the XML document containing the stylesheet was the initial source document.However, you don't need to use
xsl:variable
for this. You could specify your map node directly underxsl:stylesheet
, but you must remember that a top level elements must have a non null namespace URI:<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="some.uri" exclude-result-prefixes="my"> <my:map> <entry key="key-1">value1</entry> <entry key="key-2">value2</entry> <entry key="key-3">value3</entry> </my:map> <xsl:template match="/"> <output> <xsl:value-of select="document('')/*/my:map/entry[@key='key-1']"/> </output> </xsl:template> </xsl:stylesheet>
这篇关于XSLT:在XSLT中创建一个地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!