基于另一个变量的XSLT元素选择 [英] XSLT element selection based on another variable

查看:14
本文介绍了基于另一个变量的XSLT元素选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是XSLT的新手,我正在尝试使用下面的XSL文件,在该文件中,我希望使用同一XSL中的另一个变量来选择特定的代码。使用Saxon-He-10.5版本JAR。有人能帮我完成这个XSL转换吗?我想在output.xml中打印所选代码的标题

<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xpath-default-namespace="http://test.org/test"
  xmlns:fn="http://www.w3.org/2005/xpath-functions" 
  xmlns:lookup="http://lookup.data"
  exclude-result-prefixes="fn lookup xsl xsi">

<xsl:variable name="key1" select="&quot;status1&quot;"/>
  <xsl:output omit-xml-declaration="yes" method="xml"/>
  <xsl:variable name="header">
    <sections>
      <code key="status1" code="123" title="STATUS1"/>
      <code key="status2" code="456" title="STATUS2"/>  
    </sections>
  </xsl:variable>

  <xsl:variable name="data" select="$header/sections/code[@key=&quot;status1&quot;]" />
  <xsl:variable name="data1" select="$header/sections/code[@key=$key1]" />
  
  <xsl:template match="/">
        :<xsl:value-of select="$header" />:
        :<xsl:value-of select="$data" />:
        :<xsl:value-of select="$data/@title" />:
        :<xsl:value-of select="$data1/@title" />:
  </xsl:template>
     
</xsl:transform>

尝试使用命令提示符进行转换,如下所示:

java -cp saxon-he-10.5.jar net.sf.saxon.Transform Response.xml test.xsl > output.xml

推荐答案

设置xpath-default-namespace会导致代码失败。如果您确实需要GLOBAL声明,则需要在本地覆盖它,如<xsl:variable name="data" select="$header/sections/code[@key=&quot;status1&quot;]" xpath-default-namespace=""/>所示。

这篇关于基于另一个变量的XSLT元素选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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