小编Shi*_*hil的帖子

foreach里面标记化

这是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>
Run Code Online (Sandbox Code Playgroud)

条件:

如果Cell2包含逗号分割值并检查前面是否Cell2包含相同的值,同样如果Cell2包含空格则拆分值并检查前面是否Cell2包含相同的值 - >如果是,则忽略它.

这就是我想要输出的方式:

<Cell2>A</Cell2>
<Cell2>B</Cell2>
<Cell2>Y</Cell2>
<Cell2>X</Cell2>
Run Code Online (Sandbox Code Playgroud)

这是我写的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 …
Run Code Online (Sandbox Code Playgroud)

xslt xslt-2.0 xpath-2.0

5
推荐指数
2
解决办法
7369
查看次数

标签 统计

xpath-2.0 ×1

xslt ×1

xslt-2.0 ×1