本文介绍了xslt 根据单个测试在范围内保留多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多变量,只有两种情况.我的原始方法超出范围:

I have many variables and only two cases.My original approach goes out of scope:

<xsl:choose>
    <xsl:when test='$something = 6'>
        <xsl:variable name="foo">x</xsl:variable>
        <!-- 50 other variables -->
    </xsl:when>
    <xsl:when test='$something = 7'>
        <xsl:variable name="foo">y</xsl:variable>
        <!-- 50 other variables -->
    </xsl:when>
</xsl:choose>

即.后来,对于撒克逊人,XPST0008:变量 x 尚未声明(或其声明不在范围内)

ie. later, with saxon, XPST0008: Variable x has not been declared (or its declaration is not in scope)

我认为如果我在 xsl:variable 标签内选择它会起作用,但是测试会一遍又一遍地重复:

I think it would work if I would choose inside an xsl:variable tag, but then the tests would be repeated over and over and over and over and over:

<xsl:variable name="foo">
    <xsl:choose>
      <xsl:when test='$something = 6'>x</xsl:when>
      <xsl:when test='$something = 7'>y</xsl:when>
    </xsl:choose>
</xsl:variable>
<!-- 50 other variables, the two tests repeated for each... -->

有没有办法既保持变量在范围内又不重复自己?

根据要求添加完整的 'sscce' 文件

adding the complete 'sscce' files on request

原始方法:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:variable name="something">6</xsl:variable>
    <xsl:choose>
        <xsl:when test="$something = '6'">
            <xsl:variable name="foo">x</xsl:variable>
            <!-- 50 other variables -->
        </xsl:when>
        <xsl:when test="$something = '7'">
            <xsl:variable name="foo">y</xsl:variable>
            <!-- 50 other variables -->
        </xsl:when>
    </xsl:choose>
    <xsl:value-of select="$foo"/>
</xsl:template>
</xsl:stylesheet>

有效但强迫自己重复的方法:

approach that works but forces to repeat myself:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:variable name="something">6</xsl:variable>
    <xsl:variable name="foo">
        <xsl:choose>
          <xsl:when test='$something = 6'>x</xsl:when>
          <xsl:when test='$something = 7'>y</xsl:when>
        </xsl:choose>
    </xsl:variable>
    <!-- 50 other variables, the two tests repeated for each... -->
    <xsl:value-of select="$foo"/>
</xsl:template>
</xsl:stylesheet>

示例 xml 文件:.示例 saxon 命令行:java -jar saxon9he.jar -s:in.xml -xsl:in.xsl -o:out.html

example xml file: <xml/>. example saxon command-line: java -jar saxon9he.jar -s:in.xml -xsl:in.xsl -o:out.html

推荐答案

好吧,我宁愿那样处理:

Well, I'd rather process that way :

<xsl:variable name="something">6</xsl:variable>

<xsl:variable name="var.set">
  <xsl:choose>
    <xsl:when test="$something = '6'">
        <foo>x</foo>
        <bar>xx</bar>
        <!-- 50 other variables, to be inserted as tag -->
    </xsl:when>
    <xsl:when test="$something = '7'">
        <foo>y</foo>
        <bar>yy</bar>
        <!-- 50 other variables, to be inserted as tag -->
    </xsl:when>
  </xsl:choose>
</xsl:variable>

变量 var.set 将是一个节点集,由于 exsl:node-set() 扩展,您将能够读取它.

The variable var.set will be a nodeset that you will be able to read thanks to the exsl:node-set() extension.

例如要检索 foo 的值(存储为节点而不是变量),使用类似:<xsl:value-of select="exsl:节点集($var.set)/foo"/>.像这样,您将处理单个变量,就好像它是一个值数组.

For example to retrieve the value for foo (stored as a node and not as a variable anymore), use something like: <xsl:value-of select="exsl:node-set($var.set)/foo" />. Like this you will handle a single variable, quite as if it were an array of values.

PS:在样式表的根标签上,不要忘记添加 exsl 命名空间声明 xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"

PS: on the root tag of your stylesheet, do not forget to add the exsl namespace declaration xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"

这篇关于xslt 根据单个测试在范围内保留多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:43