【问题标题】:foreach inside tokenizeforeach 内部标记化
【发布时间】:2012-09-27 10:17:19
【问题描述】:

这是来自 xml 的 sn-p:

<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 包含逗号拆分值并检查前面的Cell2 是否包含相同的值,同样如果Cell2 包含空格拆分值并检查前面的Cell2 是否包含相同的值 -> 如果所以忽略它。

这就是我想要的输出:

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

这是我写的xslt:(请看评论)

<xsl:template match="sample">
        <xsl:for-each select="test">
                <xsl:choose>
                    <xsl:when test="contains(Cell2,',')">
                        <xsl:variable name="token1Cell2" select="tokenize(Cell2,',')"/>
                        <xsl:for-each select="$token1Cell2">
                            <xsl:choose>
                                <xsl:when test="contains(.,' ')">
                                    <xsl:variable name="token2Cell2" select="tokenize(.,' ')"/>
                                    <xsl:for-each select="$token2Cell2">
              <!-- I tried to check if the preceding sibling element test/Cell2 contains the text that is not equal to the current text. But I know what I am doing is wrong as I am inside for-each tokenize. How to get the preceding-sibling? -->
                                      <xsl:if test="preceding-sibling::test/
Cell2/text() != '.'">
                                        <Cell2>
                                            <xsl:value-of select="."/>
                                        </Cell2>
                                        </xsl:if>
                                    </xsl:for-each>
                                </xsl:when>
                                <xsl:otherwise>
                                <xsl:if test="preceding-sibling::test/Cell2/text() != '.'">
                                    <Cell2>
                                        <xsl:value-of select="."/>
                                    </Cell2>
                                </xsl:if>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <Cell2>
                            <xsl:value-of select="Cell2"/>
                        </Cell2>
                    </xsl:otherwise>
                </xsl:choose>
        </xsl:for-each>
    </xsl:template>

我怎样才能实现输出?有什么想法吗??

【问题讨论】:

    标签: xslt xslt-2.0 xpath-2.0


    【解决方案1】:

    就是这个简单的转换

    <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>
    

    应用于提供的 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>
    

    产生想要的正确结果:

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

    解释

    xsl:for-eachselect属性中的XPath表达式是理解的关键:

    distinct-values(/*/test/Cell2/tokenize(.,'[ ,]'))
    

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

    【讨论】:

      【解决方案2】:

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

      <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>
      

      【讨论】:

      • TimC,版本属性必须为“2.0”。还有@Shil,有一个更简单的解决方案。
      • 啊,是的,我已经更正了。是的,您的解决方案要简单得多!
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 2010-12-13
        • 1970-01-01
        相关资源
        最近更新 更多