简体   繁体   English

使用 XSLT-1.0 按第一个 0 值分组的 CSV 到 XML

[英]CSV to XML using XSLT-1.0 group by first 0 value

I would like to use the XSLT 1.0 engine to transform a CSV file to an XML document.我想使用 XSLT 1.0 引擎将 CSV 文件转换为 XML 文档。 The CSV file contains documents for each customer with a flexible amount of document lines. CSV 文件包含具有灵活数量的文档行的每个客户的文档。 Each new document starts with 0.每个新文档都以 0 开头。

CSV example: CSV 示例:

<root>
0;15-01-2022;Customer1
1;Dual monitors;50
2;Laser mouse;10.50
0;21-1-2022;Customer5
1;Multi-jet printer;100
0;30-1-2022;Customer8
1;Goods returned;-200
2;Basic keyboard;300
</root>

Here's the result XML document I would like to get:这是我想要得到的结果XML 文档

<Documents>
   <Document>
      <Header>
         <Customer>Customer1</Customer>
         <Date>15-01-2022</Date>
      </Header>
      <Lines>
         <Line>
            <LineNumber>1</LineNumber>
            <Price>50</Price>
            <Description>Dual monitors</Description>
         </Line>
         <Line>
            <LineNumber>2</LineNumber>
            <Price>10.50</Price>
            <Description>Laser mouse</Description>
         </Line>
      </Lines>
   </Document>
   <Document>
      <Header>
         <Customer>Customer5</Customer>
         <Date>21-1-2022</Date>
      </Header>
      <Lines>
         <Line>
            <LineNumber>1</LineNumber>
            <Price>100</Price>
            <Description>Multi-jet printer</Description>
         </Line>
      </Lines>
   </Document>
   <Document>
      <Header>
         <Customer>Customer8</Customer>
         <Date>30-1-2022</Date>
      </Header>
      <Lines>
         <Line>
            <LineNumber>1</LineNumber>
            <Price>-200</Price>
            <Description>Goods returned</Description>
         </Line>
         <Line>
            <LineNumber>2</LineNumber>
            <Price>300</Price>
            <Description>Basic keyboard</Description>
         </Line>
      </Lines>
   </Document>
</Documents>

I would like to group the document lines to the XML, but my problem is that I can't find a working method in the for-each line section.我想将文档行分组到 XML,但我的问题是我在 for-each line 部分中找不到工作方法。

XSLT I tried: XSLT我试过:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:key name="k1" match="row" use="cell[1]"/>
    <xsl:template match="/">
        <!-- tokenize csv -->
        <xsl:variable name="rows">
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="root"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="data">
            <xsl:for-each select="exsl:node-set($rows)/row[position() > 0]">
                <row>
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="text" select="."/>
                        <xsl:with-param name="delimiter" select="';'"/>
                        <xsl:with-param name="name" select="'cell'"/>
                    </xsl:call-template>
                </row>
            </xsl:for-each>
        </xsl:variable>
        <!-- output -->
        <Documents>
            <xsl:for-each select="exsl:node-set($data)/row[cell[1] = 0]">
                <Document>
                    <Header>
                        <Customer>
                            <xsl:value-of select="cell[3]"/>
                        </Customer>
                        <Date>
                            <xsl:value-of select="cell[2]"/>
                        </Date>
                    </Header>
                    <Lines>
                        <xsl:for-each select="exsl:node-set($data)/row[cell[1] > 0]">
                            <Line>
                                <LineNumber>
                                    <xsl:value-of select="cell[1]"/>
                                </LineNumber>
                                <Price>
                                    <xsl:value-of select="cell[3]"/>
                                </Price>
                                <Description>
                                    <xsl:value-of select="cell[2]"/>
                                </Description>
                            </Line>
                        </xsl:for-each>
                    </Lines>
                </Document>
            </xsl:for-each>
        </Documents>
    </xsl:template>
    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="delimiter" select="'&#10;'"/>
        <xsl:param name="name" select="'row'"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)"/>
        <xsl:if test="$token">
            <xsl:element name="{$name}">
                <xsl:value-of select="$token"/>
            </xsl:element>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
                <xsl:with-param name="delimiter" select="$delimiter"/>
                <xsl:with-param name="name" select="$name"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Unfortunately this template will not stop with the lines before the next "0" value.不幸的是,这个模板不会在下一个“0”值之前的行停止。 I don't need to sum the prices yet.我还不需要总结价格。 If anyone has any ideas on how I could achieve this that would be greatly appreciated!如果有人对我如何实现这一目标有任何想法,将不胜感激! Unfortunately I'm limited to the XSLT 1.0 version.不幸的是,我仅限于 XSLT 1.0 版本。

How about:怎么样:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" 
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="lines" match="row[not(starts-with(., '0;'))]" use="generate-id(preceding-sibling::row[starts-with(., '0;')][1])" />

<xsl:template match="/">
    <!-- tokenize csv -->
    <xsl:variable name="rows">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="/root"/>
        </xsl:call-template>
    </xsl:variable>
    <!-- output -->
    <Documents>
        <xsl:for-each select="exsl:node-set($rows)/row[starts-with(., '0;')]"> 
            <Document>
                <Header>
                    <Customer>
                        <xsl:value-of select="substring-before(substring-after(., ';'), ';')"/>
                    </Customer>
                    <Date>
                        <xsl:value-of select="substring-after(substring-after(., ';'), ';')"/>
                    </Date>
                </Header>
                <Lines>
                    <xsl:for-each select="key('lines', generate-id())"> 
                        <Line>
                            <LineNumber>
                                <xsl:value-of select="substring-before(., ';')"/>
                            </LineNumber>
                            <Price>
                                <xsl:value-of select="substring-after(substring-after(., ';'), ';')"/>
                            </Price>
                            <Description>
                                <xsl:value-of select="substring-before(substring-after(., ';'), ';')"/>
                            </Description>
                        </Line>
                    </xsl:for-each>
                </Lines>
            </Document>
         </xsl:for-each>
    </Documents>    
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&#10;'"/>
    <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
    <xsl:if test="$token">
        <row>
            <xsl:value-of select="$token"/>
        </row>
    </xsl:if>
    <xsl:if test="contains($text, $delimiter)">
        <!-- recursive call -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

If you like, you could further tokenize each row to cells - but with only 3 cells per row it's not really necessary.如果您愿意,您可以进一步将每一行标记为单元格 - 但每行只有 3 个单元格,这并不是真正必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM