xslt - foreach内部标记化

标签 xslt xslt-2.0 xpath-2.0

这是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包含逗号分割值,并检查前面的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>

如何实现输出?有任何想法吗??

最佳答案

只是这个简单的转换:

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

解释:
selectxsl:for-each属性中的XPath表达式是理解的关键:
distinct-values(/*/test/Cell2/tokenize(.,'[ ,]'))

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

关于xslt - foreach内部标记化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12619076/

相关文章:

xslt - 如何使用 XSLT 添加父节点

xslt - 如何在 XSLT 2.0 中使用 xpath 函数?

xml - 如何检查元素节点是否包含xsl中的特定值

java - 除了 xml 之外,还向 Web 浏览器提供 xsl

xslt - sharepoint dataview webpart xslt 按列的左侧字符分组

xslt - 可以用 xsd :dayTimeDuration() 指定的最长持续时间

java - XSLT 2.0 - 迭代 Java 对象列表

多层次元素的XPath?

xslt - XSL if test starts-with

python - (不是这样)子节点到列表的高级 xsl 转换