簡體   English   中英

XSLT按條件分組標記化值

[英]XSLT Grouping tokenized values by condition

我在將重復元素分組到新的xml時遇到困難。

我的輸入文件是

<Load>
    <DataArea>
        <tag>
            <row>A,Header</row>
        </tag>

        <tag>
            <row>B,20190701</row>
        </tag>
        <tag>
            <row>C,12345, 100.00, 200.00</row>
        </tag>
        <tag>
            <row>D,001, 25.00</row>
        </tag>
        <tag>
            <row>D,002, 35.00</row>
        </tag>
        <tag>
            <row>D,003, 45.00</row>
        </tag>

        <tag>
            <row>B,20190702</row>
        </tag>
        <tag>
            <row>C,12345, 300.00, 400.00</row>
        </tag>
        <tag>
            <row>D,004, 55.00</row>
        </tag>
        <tag>
            <row>D,005, 65.00</row>
        </tag>
        <tag>
            <row>D,006, 75.00</row>
        </tag>

    </DataArea>
</Load>

我必須標記逗號分隔的元素值,並想將其轉換為下面的xml結構。

<Load>
    <DataArea>
        <Header>
            <A>Header</A>
        </Header>

        <Line>
            <!-- July 1 Record -->
            <B>20190701</B>
            <C>12345</C>
            <D>
                <code>001</code>
                <amount>25.00</amount>
            </D>
            <D>
                <code>002</code>
                <amount>35.00</amount>
            </D>
            <D>
                <code>003</code>
                <amount>45.00</amount>
            </D>
        </Line>

        <Line>
            <!-- July 2 Record -->
            <B>20190702</B>
            <C>12345</C>
            <D>
                <code>004</code>
                <amount>55.00</amount>
            </D>
            <D>
                <code>005</code>
                <amount>65.00</amount>
            </D>
            <D>
                <code>006</code>
                <amount>75.00</amount>
            </D>
        </Line>
    </DataArea>
</Load>

有2個<Lines></Lines>因為只有兩個日期7月1日7月2日 日期表示為元素<B>

每個<Line>都有多個<D> <Line>

我的問題是我不能將<B><C><D>組合在一起,並用<Line>括起來。 <D>應該屬於正確的<B>或日期。

下面是我的xslt代碼。

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="/">
        <Load>
            <DataArea>
                <Header>
                    <A>
                        <xsl:for-each select="Load/DataArea/tag">
                            <xsl:variable name="col"
                                select="tokenize(current(),',')" />
                            <xsl:if test="$col[1] = 'A' ">
                                <xsl:value-of select="$col[2]" />
                            </xsl:if>
                        </xsl:for-each>

                    </A>
                </Header>

                <xsl:for-each select="Load/DataArea/tag">
                    <xsl:variable name="column"
                        select="tokenize(current(),',')" />
                    <Line>
                        <xsl:if test="$column[1] = 'B' ">
                            <B>
                                <xsl:value-of select="$column[2]" />
                            </B>
                        </xsl:if>
                        <xsl:if test="$column[1] = 'C' ">
                            <C>
                                <xsl:value-of select="$column[2]" />
                            </C>
                        </xsl:if>
                        <xsl:for-each select="../tag">
                            <xsl:variable name="column"
                                select="tokenize(current(),',')" />
                            <xsl:if test="$column[1] = 'D' ">
                                <D>
                                    <code>
                                        <xsl:value-of select="$column[2]" />
                                    </code>
                                    <amount>
                                        <xsl:value-of select="$column[3]" />
                                    </amount>
                                </D>
                            </xsl:if>
                        </xsl:for-each>
                    </Line>
                </xsl:for-each>
            </DataArea>
        </Load>
    </xsl:template>
</xsl:stylesheet>

我得到每個循環的所有<D>而不是僅屬於Date / <B>那些循環,重復所有<D>的和<Line>

我不確定如何解決這個問題,特別是我需要標記它們。

我將不勝感激。

謝謝。

我認為這可能是具有group-starting-with屬性的xsl:for-each-group的工作。

試試這個XSLT,我也簡化了使用基本的starts-with功能獲取A標頭的過程

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <Load>
      <DataArea>
        <Header>
          <A>
            <xsl:value-of select="substring-after(Load/DataArea/tag[starts-with(row, 'A,')], ',') " />
          </A>
        </Header>          
        <xsl:for-each-group select="Load/DataArea/tag[not(starts-with(row, 'A,'))]" group-starting-with="tag[starts-with(row, 'B,')]">
          <Line>
            <xsl:for-each select="current-group()">
              <xsl:variable name="column" select="tokenize(row,',')" />
              <xsl:element name="{$column[1]}">
              <xsl:choose>
                <xsl:when test="$column[1] = 'B' or $column[1] = 'C'">
                  <xsl:value-of select="$column[2]" />
                </xsl:when>
                <xsl:otherwise>
                  <code>
                    <xsl:value-of select="$column[2]" />
                  </code>
                  <amount>
                    <xsl:value-of select="$column[3]" />
                  </amount>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:element>
            </xsl:for-each>
          </Line>
        </xsl:for-each-group>
      </DataArea>
    </Load>
  </xsl:template>
</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM