简体   繁体   English

XSLT - 条件变量未分配数组值

[英]XSLT - variable in condition is not assigned with array value

Using xslt3 (Saxon JS) and not bound to a specific XSLT version使用 xslt3 (Saxon JS) 并且未绑定到特定的 XSLT 版本

Given this sample.xml给定这个样本。xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<tasks>
    <task id="id0"/>
    <task id="id1" parent="#id0" deps="#id2 #id3 #id0"/>
    <task id="id2" parent="#id0"/>
    <task id="id3" parent="#id0"/>
</tasks>

and this sample.xsl这个sample.xsl

<?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:for-each select="/tasks/task">
            <xsl:variable name="parentTask" select="replace(@parent,'#','')"/>
            
            <xsl:variable name="depTasks">
                <xsl:choose>
                    <xsl:when test="@deps">

                        <xsl:value-of select="tokenize(replace(@deps,'#',''),'\s')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$parentTask"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <parent id="{@id}">
                <xsl:for-each select="/tasks/task[@id=$depTasks]">
                    <dep>
                        <xsl:value-of select="@id"/>
                    </dep>
                </xsl:for-each>
            </parent>

        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I would expect the $depTasks variable to contain an array (all the deps listed in the deps attribute) or a single value (the dep listed in the parent attribute), and obtain a transformation like this:我希望$depTasks变量包含一个数组( deps属性中列出的所有deps )或单个值( parent属性中列出的dep ),并获得如下转换:

<?xml version="1.0" encoding="UTF-8"?>
<parent id="id0"/>
<parent id="id1">
    <dep>id2</dep>
    <dep>id3</dep>
    <dep>id0</dep>
</parent>
<parent id="id2">
    <dep>id0</dep>
</parent>
<parent id="id3">
    <dep>id0</dep>
</parent>

BUT instead I'm getting this result (it seems that only the first element of the tokenization is assigned)但是相反,我得到了这个结果(似乎只分配了标记化的第一个元素)

<?xml version="1.0" encoding="UTF-8"?>
<parent id="id0"/>
<parent id="id1">
    <dep>id2</dep>
    <!-- MISSING DEPS -->
</parent>
<parent id="id2">
    <dep>id0</dep>
</parent>
<parent id="id3">
    <dep>id0</dep>
</parent>

Do problems or limitations exist for variable assigment inside conditions?条件内部变量分配是否存在问题或限制? Or am I missing something here?或者我在这里错过了什么?

Note that, if I change a bit the stylesheet and declare the variable OUTSIDE a condition, an array is assigned as I expect.请注意,如果我稍微更改样式表并将变量 OUTSIDE 声明为条件,则会按预期分配一个数组。

sample2.xsl样本2.xsl

<?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:for-each select="/tasks/task">
            <xsl:variable name="parentTask" select="replace(@parent,'#','')"/>
            <!-- VARIABLE assignement outside a condition: value is an array -->
            <xsl:variable name="depTasks" select="tokenize(replace(@deps,'#',''),'\s')"/>

            <parent id="{@id}">
                <xsl:for-each select="/tasks/task[@id=$depTasks]">
                    <dep>
                        <xsl:value-of select="@id"/>
                    </dep>
                </xsl:for-each>
            </parent>

        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

resulting in this transformation导致这种转变

<?xml version="1.0" encoding="UTF-8"?>
<parent id="id0"/>
<parent id="id1">
    <dep>id0</dep>
    <dep>id2</dep>
    <dep>id3</dep>
</parent>
<parent id="id2"/>
<parent id="id3"/>

In addition to Martin Honnen's answer:除了 Martin Honnen 的回答:

If you are using XSLT 3.0, do not declare your stylesheet as version="1.0" .如果您使用的是 XSLT 3.0,请不要将样式表声明为version="1.0" Doing so invokes backwards-compatible behavior which can significantly alter your result.这样做会调用向后兼容的行为,这可能会显着改变您的结果。

For example, the xsl:value instruction (which you are using in several places) will output the string-value of only the first node of the selected sequence, instead of all of them.例如, xsl:value指令(您在多个地方使用)将 output 仅选择序列的第一个节点的字符串值,而不是所有节点。

Your are not using any arrays in your code at all.您根本没有在代码中使用任何 arrays 。 In

      <xsl:variable name="depTasks">
            <xsl:choose>
                <xsl:when test="@deps">

                    <xsl:value-of select="tokenize(replace(@deps,'#',''),'\s')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$parentTask"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

you are creating a result tree fragment containing a text node with the string value of the tokenize call or the $parentTask variable.您正在创建一个结果树片段,其中包含一个带有tokenize调用或$parentTask变量的字符串值的文本节点。

In <xsl:variable name="depTasks" select="tokenize(replace(@deps,'#',''),'\s')"/> the value of the variable is the result of the tokenize call which is a sequence of string values.<xsl:variable name="depTasks" select="tokenize(replace(@deps,'#',''),'\s')"/>中,变量的值是tokenize调用的结果,即一系列字符串值。

If for the previous approach, although not recommeded, you wanted a sequence of string, you would need to use如果对于前面的方法,虽然不推荐,但你想要一个字符串序列,你需要使用

<xsl:variable name="depTasks" as="xs:string*">
  ...
  <xsl:sequence select="tokenize(...)"/>

  ...

</xsl:variable>

The preferred way for the more complex part would be select="if (@deps) then tokenize(...) else $parentTask" .更复杂部分的首选方法是select="if (@deps) then tokenize(...) else $parentTask"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM