XML:
<sample>
<test>
<Cell1>John</Cell1>
<Cell2>A</Cell2>
<Cell4>xy</Cell4>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>B</Cell2>
<Cell6>10</Cell6>
</test>
<test>
<Cell1>John,Jade</Cell1>
<Cell2>A,Y</Cell2>
<Cell4>1</Cell4>
</test>
<test>
<Cell1>John,Jade</Cell1>
<Cell2>A C,X</Cell2>
</test>
<test>
<Cell1>John,Jade</Cell1>
<Cell2>C D,Y</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>A B</Cell2>
<Cell4>xy</Cell4>
</test>
</sample>
XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="/">
<xsl:apply-templates select="sample"/>
</xsl:template>
<xsl:template match="sample">
<xsl:variable name="atomictest">
<!--Store the test containing only one value in cell2-->
<xsl:copy-of select="test[not(contains(Cell2,',')) or not(contains(Cell2,' '))]"/>
</xsl:variable>
<xsl:variable name="copy">
<xsl:apply-templates select="test">
<xsl:with-param name="atomictest" select="$atomictest"/>
</xsl:apply-templates>
</xsl:variable>
</xsl:template>
<xsl:template match="test">
<xsl:param name="atomictest"/>
<xsl:choose>
<xsl:when test="contains(Cell2,',')">
<xsl:variable name="Cell1">
<xsl:copy-of select="Cell1"/>
</xsl:variable>
<!-- tokenize cell2 based on comma -->
<xsl:for-each select="tokenize(Cell2,',')">
<xsl:variable name="str">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="pos">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:choose>
<!-- If cell2 contains space -->
<xsl:when test="contains(.,' ')">
<!-- tokenize cell2 based on comma -->
<xsl:for-each select="tokenize(.,' ')">
<xsl:variable name="str">
<xsl:value-of select="."/>
</xsl:variable>
<!-- if cell2 value not contained in the atomic collected -->
<xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
<!--Store Cell2 value -->
<xsl:variable name="Cell2">
<xsl:value-of select="."/>
</xsl:variable>
<!-- tokenize cell1-->
<xsl:for-each select="tokenize($Cell1/Cell1,',')">
<xsl:if test="position()=$pos">
<test>
<Cell1>
<xsl:value-of select="."/>
</Cell1>
<Cell2>
<xsl:value-of select="$Cell2"/>
</Cell2>
</test>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<!-- if cell2 doesnot contains space -->
<xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
<xsl:variable name="Cell2">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:for-each select="tokenize($Cell1/Cell1,',')">
<xsl:if test="position()=$pos">
<test>
<Cell1>
<xsl:value-of select="."/>
</Cell1>
<Cell2>
<xsl:value-of select="$Cell2"/>
</Cell2>
</test>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:when test="contains(Cell2,' ')">
<xsl:variable name="Cell1">
<xsl:copy-of select="Cell1"/>
</xsl:variable>
<!-- tokenize cell2 based on space or comma -->
<xsl:for-each select="tokenize(Cell2,' ')">
<xsl:variable name="str">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="pos">
<xsl:value-of select="position()"/>
</xsl:variable>
<!-- if cell2 value not contained in the atomic rows collected -->
<xsl:if test="not($atomictest/test[normalize-space(Cell2/text())=normalize-space($str)])">
<xsl:if test="position()=$pos">
<test>
<Cell1>
<xsl:value-of select="$Cell1"/>
</Cell1>
<Cell2>
<xsl:value-of select="$str"/>
</Cell2>
</test>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<test>
<Cell1>
<xsl:value-of select="Cell1"/>
</Cell1>
<Cell2>
<xsl:value-of select="Cell2"/>
</Cell2>
</test>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
- 私はatomictest変数に単一の値を含むcell2を保存しました
- Cell2 にコンマが含まれているかどうかを確認します。true の場合は、コンマに基づいて Cell2 をトークン化し、トークン化された Cell2 の値がアトミック テストに存在するかどうかを確認します -> 存在しない場合は、Cell2 と Cell1 の値を出力に追加します
- 出力に新しく追加された Cell1 と Cell2 の値を atomictest 変数に更新して、次回同じ Cell2 の値を通過した場合にそれをスキップする必要があるようにしたいと思います。これを行う方法??
私が得る出力:
<test>
<Cell1>John</Cell1>
<Cell2>A</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>B</Cell2>
</test>
<test>
<Cell1>Jade</Cell1>
<Cell2>Y</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>C</Cell2>
</test>
<test>
<Cell1>Jade</Cell1>
<Cell2>X</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>C</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>D</Cell2>
</test>
<test>
<Cell1>Jade</Cell1>
<Cell2>Y</Cell2>
</test>
結果の出力は次のようになります。
<test>
<Cell1>John</Cell1>
<Cell2>A</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>B</Cell2>
</test>
<test>
<Cell1>Jade</Cell1>
<Cell2>Y</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>C</Cell2>
</test>
<test>
<Cell1>Jade</Cell1>
<Cell2>X</Cell2>
</test>
<test>
<Cell1>John</Cell1>
<Cell2>D</Cell2>
</test>