来自变量的XSLT for-each-group不起作用

时间:2017-04-13 17:40:34

标签: grouping xslt-2.0 xslt-grouping

来自XSLT 2.0的

for-each-group按照预期从文件中工作,但不能从变量中工作。

有这个文件:
〜$ cat test.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <delimiter/>
    <c>A</c><c>B</c>
  <delimiter/>
    <c>C</c>
</root>

使用样式表对此文件进行分组:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0"
  encoding="UTF-8" indent="yes" omit-xml-declaration="no" />

<xsl:template match="*">

  <!-- variable not used for file test -->
  <xsl:variable name="fields">
    <root>
      <delimiter/>
      <c>A</c><c>B</c>
      <delimiter/>
      <c>C</c>
    </root>
  </xsl:variable>

  <xsl:for-each-group select="*" group-starting-with="delimiter">
    <field>
      <xsl:for-each select="current-group()">
        <xsl:value-of select="self::c"/>
      </xsl:for-each>
    </field>
  </xsl:for-each-group>

</xsl:template>

</xsl:stylesheet>

我得到了我想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<field>AB</field>
<field>C</field>

尝试将变量name =“fields”分组为:

<xsl:for-each-group select="$fields/*" group-starting-with="delimiter">

我得到了结果:

<?xml version="1.0" encoding="UTF-8"?>
<field/>

为什么for-each-group适用于文件但不适用于变量?

1 个答案:

答案 0 :(得分:2)

变量<xsl:variable name="fields" as="element()"> <root> <delimiter/> <c>A</c><c>B</c> <delimiter/> <c>C</c> </root> </xsl:variable> 是一个document-node(),你可以将变量的类型定义为element()

select format('%s  %s  %s', username, first_name, last_name)
from t
order by greatest (
    similarity(_name, username),
    similarity(_name, first_name),
    similarity(_name, last_name)
) desc
limit 10