Xslt foreach内部标记化

Xslt foreach内部标记化,xslt,xslt-2.0,xpath-2.0,Xslt,Xslt 2.0,Xpath 2.0,这是xml中的一个片段: <sample> <test> <Cell1>John</Cell1> <Cell2>A</Cell2> <Cell4>xy</Cell4> </test> <test> <Cell1>Jack&l

这是xml中的一个片段:

<sample>
        <test>
            <Cell1>John</Cell1>
            <Cell2>A</Cell2>
            <Cell4>xy</Cell4>
        </test>
        <test>
            <Cell1>Jack</Cell1>
            <Cell2>B</Cell2>
            <Cell3>Red</Cell3>
            <Cell6>10</Cell6>
        </test>
        <test>
            <Cell1>John,Jade</Cell1>
            <Cell2>A,Y</Cell2>
            <Cell4>1</Cell4>
        </test>
        <test>
            <Cell1>John,Jade</Cell1>
            <Cell2>A B,X</Cell2>
            <Cell3>Blue</Cell3>
        </test>
    </sample>
这是我写的xslt:(请查看注释)



我怎样才能获得输出?有什么想法吗?

首先,如果您在逗号或空格上进行标记化,那么您可以将标记化组合成一个表达式

<xsl:for-each select="tokenize(., ',|\s')">

您可以做的是,首先将所有Cell2元素与模板匹配,并标记内容,但将结果放入变量中

  <xsl:variable name="cells">
     <xsl:apply-templates select="test/Cell2"/>
  </xsl:variable>

然后您可以简单地使用xsl:for-each-group对它们进行迭代

  <xsl:for-each-group select="$cells/Cell2" group-by="text()">
     <xsl:copy-of select="."/>
  </xsl:for-each-group>

这是完整的XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="sample">
      <xsl:variable name="cells">
         <xsl:apply-templates select="test/Cell2"/>
      </xsl:variable>
      <xsl:for-each-group select="$cells/Cell2" group-by="text()">
         <xsl:copy-of select="."/>
      </xsl:for-each-group>
   </xsl:template>

   <xsl:template match="Cell2">
      <xsl:for-each select="tokenize(., ',|\s')">
         <Cell2>
            <xsl:value-of select="."/>
         </Cell2>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

当应用于示例XML时,将输出以下内容

<Cell2>A</Cell2>
<Cell2>B</Cell2>
<Cell2>Y</Cell2>
<Cell2>X</Cell2>
A
B
Y
X

如果对每个指令进行了深度嵌套xsl:for,则上下文项在每个级别都会发生变化,因此通常需要在每个级别将变量绑定到上下文项,以便返回到它。但是,深度嵌套的xsl:for-each指令有时表示您应该将代码分解为更小的模板或函数。

仅此简单转换即可:

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

 <xsl:template match="/">
     <xsl:for-each select="distinct-values(/*/test/Cell2/tokenize(.,'[ ,]'))">
      <Cell2><xsl:value-of select="."/></Cell2>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

这将生成所有标记化序列的不同值(字符串值)
/*/test/Cell2

TimC,版本属性必须为“2.0”。还有@Shil,有一个更简单的解决方案。啊,是的,我已经纠正了。是的,您的解决方案更简单!
<Cell2>A</Cell2>
<Cell2>B</Cell2>
<Cell2>Y</Cell2>
<Cell2>X</Cell2>
<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:for-each select="distinct-values(/*/test/Cell2/tokenize(.,'[ ,]'))">
      <Cell2><xsl:value-of select="."/></Cell2>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
<sample>
    <test>
        <Cell1>John</Cell1>
        <Cell2>A</Cell2>
        <Cell4>xy</Cell4>
    </test>
    <test>
        <Cell1>Jack</Cell1>
        <Cell2>B</Cell2>
        <Cell3>Red</Cell3>
        <Cell6>10</Cell6>
    </test>
    <test>
        <Cell1>John,Jade</Cell1>
        <Cell2>A,Y</Cell2>
        <Cell4>1</Cell4>
    </test>
    <test>
        <Cell1>John,Jade</Cell1>
        <Cell2>A B,X</Cell2>
        <Cell3>Blue</Cell3>
    </test>
</sample>
<Cell2>A</Cell2>
<Cell2>B</Cell2>
<Cell2>Y</Cell2>
<Cell2>X</Cell2>
distinct-values(/*/test/Cell2/tokenize(.,'[ ,]'))