XSLT:在XSLT中创建一个地图 [英] XSLT : Creating a Map in XSLT

查看:104
本文介绍了XSLT:在XSLT中创建一个地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xsl中有一个键值映射,所以定义了一个具有xml片段的变量,但是后来当我尝试访问变量中的xml节点时,我得到一个错误,类型的xpath不能被解析。

 < 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 under xsl: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆