XSLT在创建时读取/更改结果树

Chr*_*ris 1 xslt resultset

我正在尝试在变量中创建项目列表,并且想要检查项目是否已经在列表中.这些项目由输入文档给出,这对我的问题并不重要.

所以我试图解决两个问题.

1:从我当前生成的变量中读取数据

2:将文本追加到变量中的节点

这是我到目前为止所尝试的:

<xsl:variable name="data">
    <list>
        <xsl:call-template name="generate"/>
    </list>
</xsl:variable>


<xsl:template name="generate" match="/">
    <xsl:apply-templates select="//thing"/>
</xsl:template>


<xsl:template match="//thing">
    <xsl:variable name="temp">
        <xsl:value-of select="./text()"/>
    </xsl:variable>

    <xsl:choose>
        <!-- test however this thing is already in $data -->
        <!-- problem #1: here, trying to read anything out of $data doesn't work -->
        <xsl:when test="contains($data/list/item/text(), $temp/text())">
            <xsl:for-each select="$data/list/item">
                <xsl:if test="contains(./text(), $temp/text())">
                    <!-- problem #2: append any string to self::node()/text() -->
                </xsl:if>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <item>
                <xsl:value-of name="$temp/text()"/>
            </item>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

我已经搜索了很多结果树片段等等,但是没有想到如何做到这一点.

编辑:这应该是输入代码:

<data>
<thing>string1</thing>
<thing>string2</thing>
<thing>string3</thing>
<nested><thing>string1</thing></nested>
<nested><thing>string1</thing></nested>
<nested><thing>string2</thing></nested>
</data>
Run Code Online (Sandbox Code Playgroud)

输出:

<list>
<item>string1 string1 string1</item>
<item>string2 string2</item>
<item>string3</item>
</list>
Run Code Online (Sandbox Code Playgroud)

克里斯

Dim*_*hev 5

这是我的答案(独立于Martin Honen),但我意识到它没有回答你的主要问题 - 如何遍历树并处理和累积数据,其中处理也使用已经累积的数据.我在答案的第二部分提供了这个想要的解决方案..

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

 <xsl:key name="kThingByVal" match="thing"
  use="."/>

 <xsl:template match="/">
     <list>
      <xsl:apply-templates select=
      "//thing
          [generate-id()
          =
           generate-id(key('kThingByVal', .)[1])
          ]
      ">
      </xsl:apply-templates>
     </list>
 </xsl:template>

 <xsl:template match="thing">
  <item>
   <xsl:apply-templates mode="gen"
        select="key('kThingByVal', .)"/>
  </item>
 </xsl:template>

 <xsl:template match="thing" mode="gen">
  <xsl:if test="not(position() = 1)">
   <xsl:text> </xsl:text>
  </xsl:if>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的XML文档时:

<data>
    <thing>string1</thing>
    <thing>string2</thing>
    <thing>string3</thing>
    <nested>
        <thing>string1</thing>
    </nested>
    <nested>
        <thing>string1</thing>
    </nested>
    <nested>
        <thing>string2</thing>
    </nested>
</data>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<list>
   <item>string1 string1 string1</item>
   <item>string2 string2</item>
   <item>string3</item>
</list>
Run Code Online (Sandbox Code Playgroud)

II.这个问题属于这类问题的一般解决方案:

我们希望能够像处理f:foldl()FXSL 这样的函数一样处理树.

这是一个经典的应用程序,展示了如何f:foldl()(在XSLT 1.0的情况下,这是一个模板,而不是一个xsl:function)可以处理任何带有当前累积结果和curent list-item的函数的列表,并产生新的结果:

testFoldl.xsl:

<xsl:stylesheet version="1.0"
xmlns:f="http://fxsl.sf.net/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foldr-func="foldr-func"
exclude-result-prefixes="xsl f foldr-func"
>

   <xsl:import href="foldl.xsl"/>

   <!-- This transformation must be applied to:
        numList.xml 
     -->
   <foldr-func:foldr-func/>
   <xsl:variable name="vFoldrFun" select="document('')/*/foldr-func:*[1]"/>
    <xsl:output  encoding="UTF-8" omit-xml-declaration="yes"/>

    <xsl:template match="/">

      <xsl:call-template name="foldl">
        <xsl:with-param name="pFunc" select="$vFoldrFun"/>
        <xsl:with-param name="pList" select="/*/*"/>
        <xsl:with-param name="pA0" select="0"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template mode="f:FXSL"
      match="foldr-func:*">
         <xsl:param name="arg1" select="0"/>
         <xsl:param name="arg2" select="0"/>

         <xsl:value-of select="$arg1 + $arg2"/>
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于此XML文档(表示数字列表)时:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>
Run Code Online (Sandbox Code Playgroud)

生成所需结果(所有列表项的总和):

55
Run Code Online (Sandbox Code Playgroud)

请注意,如果我们作为参数传递而不是add()函数,而是mult()函数(并0从列表中删除,指定一个新的"零"参数 - 1- 乘法的中性数,我们将得到所有列表项的乘积- 10!(十阶乘)

因此,f:foldl()是一个非常强大的功能,可用于处理任何基于累积结果和当前列表项的列表,产生新的结果.

如果这个话题似乎有趣的你,有很多更有趣的东西在这里.

有趣的是,存在一个处理树的类似函数:

fold-tree2.xsl:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="f ext msxsl xsl"
>
 <xsl:template name="foldl-tree2">
  <xsl:param name="pFuncNode" select="/.."/>
  <xsl:param name="pFuncSubtrees" select="/.."/>
  <xsl:param name="pA0"/>
  <xsl:param name="pNode" select="/.."/>

  <xsl:choose>
   <xsl:when test="not($pNode)">
            <xsl:copy-of select="$pA0"/>
   </xsl:when>

   <xsl:otherwise>
    <xsl:variable name="vSubtrees" select="$pNode/*"/>

    <xsl:variable name="vSubTreeResult">
      <xsl:call-template name="foldl-tree_">
        <xsl:with-param name="pFuncNode" select="$pFuncNode"/>
        <xsl:with-param name="pFuncSubtrees" select="$pFuncSubtrees"/>
        <xsl:with-param name="pA0" select="$pA0"/>
        <xsl:with-param name="pSubTrees" select="$vSubtrees"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:apply-templates select="$pFuncNode[1]" mode="f:FXSL">
     <xsl:with-param name="arg0" select="$pFuncNode[position() > 1]"/>
     <xsl:with-param name="arg1" select="$pNode"/>
     <xsl:with-param name="arg2" select="ext:node-set($vSubTreeResult)"/>
    </xsl:apply-templates>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="foldl-tree_">
  <xsl:param name="pFuncNode" select="/.."/>
  <xsl:param name="pFuncSubtrees" select="/.."/>
  <xsl:param name="pA0"/>
  <xsl:param name="pSubTrees" select="/.."/>

  <xsl:choose>
   <xsl:when test="not($pSubTrees)">
            <xsl:copy-of select="$pA0"/>
   </xsl:when>
   <xsl:otherwise>
      <xsl:variable name="vSubTree1Result">
        <xsl:call-template name="foldl-tree2">
          <xsl:with-param name="pFuncNode" select="$pFuncNode"/>
          <xsl:with-param name="pFuncSubtrees" select="$pFuncSubtrees"/>
          <xsl:with-param name="pA0" select="$pA0"/>
          <xsl:with-param name="pNode" select="$pSubTrees[1]"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="vRestSubtreesResult">
        <xsl:call-template name="foldl-tree_">
          <xsl:with-param name="pFuncNode" select="$pFuncNode"/>
          <xsl:with-param name="pFuncSubtrees" select="$pFuncSubtrees"/>
          <xsl:with-param name="pA0" select="$pA0"/>
          <xsl:with-param name="pSubTrees" select="$pSubTrees[position() > 1]"/>
        </xsl:call-template>
      </xsl:variable>

     <xsl:apply-templates select="$pFuncSubtrees" mode="f:FXSL">
      <xsl:with-param name="arg0" select="$pFuncSubtrees[position() > 1]"/>
      <xsl:with-param name="arg1" select="ext:node-set($vSubTree1Result)"/>
      <xsl:with-param name="arg2" select="ext:node-set($vRestSubtreesResult)"/>
    </xsl:apply-templates>
   </xsl:otherwise>
  </xsl:choose>    
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这里,除了树,零(初始结果)和处理每个树节点的函数(连同累积结果),我们还作为参数传递一个函数,该函数接受树节点的所有子树的集合和到目前为止累积的结果,并产生新的结果.此外,我们必须作为参数传递树的顶部节点.

以下是使用的简单说明foldl-tree2():

test-foldlTree2.xsl:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:add-tree="add-tree"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="xsl f add-tree"
>
    <xsl:import href="foldl-tree2.xsl"/>

    <xsl:strip-space elements="*"/>
    <add-tree:add-tree/>

    <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:variable name="vAdd" select="document('')/*/add-tree:*[1]"/>

      <xsl:call-template name="foldl-tree2">
        <xsl:with-param name="pFuncNode" select="$vAdd"/>
        <xsl:with-param name="pFuncSubtrees" select="$vAdd"/>
        <xsl:with-param name="pA0" select="0"/>
        <xsl:with-param name="pNode" select="/*"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template match="add-tree:*" mode="f:FXSL">
      <xsl:param name="arg1"/>
      <xsl:param name="arg2"/>

      <xsl:variable name="varg1">
        <xsl:call-template name="nodeValue">
         <xsl:with-param name="pNode" select="$arg1"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="varg2">
        <xsl:call-template name="accumValue">
         <xsl:with-param name="pAccum" select="$arg2"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:value-of select="$varg1 + $varg2"/>
    </xsl:template>

    <xsl:template name="nodeValue">
     <xsl:param name="pNode"/>
     <xsl:call-template name="toNumber">
      <xsl:with-param name="pX" select="$pNode/text()[1]"/>
     </xsl:call-template>
    </xsl:template>

    <xsl:template name="accumValue">
     <xsl:param name="pAccum"/>
     <xsl:call-template name="toNumber">
      <xsl:with-param name="pX" select="$pAccum"/>
     </xsl:call-template>
    </xsl:template>

    <xsl:template name="toNumber">
     <xsl:param name="pX"/>

     <xsl:choose>
      <xsl:when test="not(number($pX) = number($pX))">0</xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="number($pX)"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当在任何树上应用此转换时,其某些文本节点包含数字:

<nums>
  <a>
   <b>
    <num>01</num>
   </b>
  </a>
  <c>
   <num>02</num>
   <num>03</num>
   <num>04</num>
   <d>
    <num>05</num>
    <num>06</num>
   </d>
  </c>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>
Run Code Online (Sandbox Code Playgroud)

它产生所有这些数字的总和:

55
Run Code Online (Sandbox Code Playgroud)

现在,我们只需要传递不同的参数foldl-tree2(),它将提取所有thing元素值的出现列表,并根据item原始问题的要求将所有相同的值放在一个单独的值下:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:ext="http://exslt.org/common"
 xmlns:merge-list="merge-list"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="xsl f ext msxsl merge-list"
>
    <xsl:import href="foldl-tree2.xsl"/>

    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <merge-list:merge-list/>

    <xsl:variable name="vrtfZero">
     <list/>
    </xsl:variable>

    <xsl:variable name="vZero" select=
     "document('')/*/xsl:variable[@name='vrtfZero']/*
     "/>

    <xsl:template match="/">
      <xsl:variable name="vFunMerge" select="document('')/*/merge-list:*[1]"/>

      <list>
       <xsl:call-template name="foldl-tree2">
        <xsl:with-param name="pFuncNode" select="$vFunMerge"/>
        <xsl:with-param name="pFuncSubtrees" select="$vFunMerge"/>
        <xsl:with-param name="pA0" select="$vZero"/>
        <xsl:with-param name="pNode" select="/*"/>
       </xsl:call-template>
      </list>
    </xsl:template>

    <xsl:template match="merge-list:*" mode="f:FXSL">
      <xsl:param name="arg1"/>
      <xsl:param name="arg2"/>

       <xsl:variable name="vrtfArg1">
        <xsl:apply-templates mode="gen" select="$arg1"/>
       </xsl:variable>

       <xsl:variable name="vrtfArg2">
        <xsl:apply-templates mode="gen" select="$arg2"/>
       </xsl:variable>

       <xsl:variable name="vArg1" select="ext:node-set($vrtfArg1)/*"/>
       <xsl:variable name="vArg2" select="ext:node-set($vrtfArg2)/*"/>

       <xsl:for-each select="$vArg1[self::thing or self::item]">
        <xsl:variable name="vMatch" select=
         "$vArg2[self::thing or self::item
               and
                 substring-before(concat(., ' '), ' ')
                =
                 substring-before(concat(current(), ' '), ' ')
                ]"/>
        <xsl:choose>
          <xsl:when test="$vMatch">
            <item>
             <xsl:value-of select="$vMatch/text()"/>
             <xsl:text> </xsl:text>
             <xsl:value-of select="."/>
            </item>
          </xsl:when>
          <xsl:otherwise>
           <xsl:copy-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
       </xsl:for-each>

       <xsl:for-each select="$vArg2[self::thing or self::item]">
        <xsl:variable name="vMatch" select=
         "$vArg1[self::thing or self::item
               and
                 substring-before(concat(., ' '), ' ')
                =
                 substring-before(concat(current(), ' '), ' ')
                ]"/>
          <xsl:if test="not($vMatch)">
           <xsl:copy-of select="."/>
          </xsl:if>
       </xsl:for-each>
    </xsl:template>

    <xsl:template match="thing" mode="gen">
     <item><xsl:value-of select="."/></item>
    </xsl:template>

    <xsl:template match="item" mode="gen">
     <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="*" mode="gen"/>

    <xsl:template mode="gen" match="/">
     <xsl:apply-templates select="*" mode="gen"/>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于最初提供的XML文档时:

<data>
    <thing>string1</thing>
    <thing>string2</thing>
    <thing>string3</thing>
    <nested>
        <thing>string1</thing>
    </nested>
    <nested>
        <thing>string1</thing>
    </nested>
    <nested>
        <thing>string2</thing>
    </nested>
</data>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<list>
   <item>string1 string1 string1</item>
   <item>string2 string2</item>
   <item>string3</item>
</list>
Run Code Online (Sandbox Code Playgroud)