XSLT 1.0 值查找映射 [英] XSLT 1.0 Value look up Map

查看:25
本文介绍了XSLT 1.0 值查找映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手,花了相当多的时间来处理创建内嵌查找映射以将特定值替换为 XSLT 2.0 中映射列表的另一个值,结果发现我只能使用1.0.:-(

I am new to XSLT and spent a fair amount of time getting to grips with creating an in-line look up map to replace a specific value with another value for a mapped list in XSLT 2.0 only to find out I can only use 1.0. :-(

我的问题是如何在 1.0 中复制以下工作 XSLT 2.0 代码.我尝试了一些东西,但似乎无法让它工作.

My question is how can a replicate the below working XSLT 2.0 code in 1.0. I have tried a few things but can't seem to get it working.

需要注意的是,如果没有映射,那么元素应该是空的.

Point to note that if there is no map then the element should be empty.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>


<xsl:variable name="mapxml" >
<map>
<Country>
    <input value="GB">RZ</input>
    <input value="FR">TH</input>
   </Country>
</map>
 </xsl:variable>


  <xsl:variable name="vMap" 
       select="$mapxml" />


 <xsl:key name="kInputByVal" match="input" 
   use="@value" />


    <xsl:template match="Country/text()">
        <xsl:sequence select= 
         "(key('kInputByVal', ., $vMap/*/Country)[1]/text()
           )[1]
         "/>

 </xsl:template>
</xsl:stylesheet>

输入 XML:

 <user>
        <Country>GB</Country>
        <Name>FOO</Name>
        <Address>BAR</Address>
<user>

推荐答案

这里是等效的 XSLT 1.0 程序:

Here is the equivalent XSLT 1.0 program:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://tempuri.org/dummy"
  exclude-result-prefixes="my"
>
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <my:config>
    <map>
      <Country>
        <input value="GB">RZ</input>
        <input value="FR">TH</input>
      </Country>
    </map>
  </my:config>
  <xsl:variable name="vCountryMap" select="document('')/*/my:config/map/Country/input" />

  <xsl:template match="Country/text()">
    <xsl:value-of select="$vCountryMap[@value = current()]" />
  </xsl:template>
</xsl:stylesheet>

注意事项:

  • 您可以在 XSLT 中设置额外的节点,因为 XSLT 本身就是 XML.例如对于配置数据,就像这里.您只需要确保为它们使用不同的命名空间.
  • 命名空间 URI 必须是唯一的,它们不需要指向现有文档.像 http://tempuri.org/dummy 这样的临时 URI 完全没问题.
  • <xsl:strip-space elements="*"/> 指示 XSLT 处理器忽略输入中无关紧要的空白.这有助于创建整齐缩进的输出.
  • document('') 指的是 XSLT 样式表本身.如果需要,您还可以将配置保存在一个额外的 XML 文件中.
  • exclude-result-prefixes 防止我们的临时命名空间泄漏到输出.
  • current() 指的是 XSLT 处理器当前正在处理的节点.($vCountryMap[@value = .] 不起作用,因为这里的 . 指的是 XPath 上下文,即 节点.)
  • You can set up extra nodes within the XSLT, simply because XSLT is XML itself. For example for configuration data, like here. You just need to make sure that you use a different namespace for them.
  • Namespace URIs need to be unique, they don't need to point to an existing document. A temporary URI like http://tempuri.org/dummy is perfectly all-right.
  • <xsl:strip-space elements="*" /> instructs the XSLT processor to ignore insignificant whitespace in the input. This helps in creating neatly indented output.
  • document('') refers to the XSLT stylesheet itself. You could also keep the configuration in an extra XML file, if you wanted to.
  • exclude-result-prefixes prevents the leaking of our temp namespace to the output.
  • current() refers to the node the XSLT processor is currently handling. ($vCountryMap[@value = .] would not work, because here the . refers to the XPath context, i.e. the <input> nodes.)

这篇关于XSLT 1.0 值查找映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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