如何处理不同长度/更多“行”的文件

时间:2019-08-12 15:10:27

标签: xslt-1.0

我的问题是,我的输入将具有不同的长度/行数。我当前的代码(感谢上一个问题的michael.hor257k)适用于我的测试文件,但我确定它不适用于包含更多项目的文件。

例如:

具有100条记录的输入文件 要么 一个包含2条记录的输入文件。

我也不确定如何忽略最后一条记录,因为它是在原始csv文件中创建的垃圾记录,该记录已转换为输入文件。

输入:

<csv-xml>
   <record line="1">
      <csv-field-1>1</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3>7654321</csv-field-3>
      <csv-field-4>1</csv-field-4>
      <csv-field-5>08/08/19</csv-field-5>
      <csv-field-6>08/08/19</csv-field-6>
   </record>
   <record line="2">
      <csv-field-1>2</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3>12345678</csv-field-3>
      <csv-field-4>3</csv-field-4>
   </record>
   <record line="3">
      <csv-field-1>2</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3>22345679</csv-field-3>
      <csv-field-4>7</csv-field-4>
   </record>
   <record line="4">
      <csv-field-1>2</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3>32345680</csv-field-3>
      <csv-field-4>6</csv-field-4>
   </record>
   <record line="5">
      <csv-field-1>2</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3>42345681</csv-field-3>
      <csv-field-4>2</csv-field-4>
   </record>
   <record line="6">
      <csv-field-1>3</csv-field-1>
      <csv-field-2>12345</csv-field-2>
      <csv-field-3></csv-field-3>
   </record>
</csv-xml>

代码:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <orders>
        <order>
            <accountNo>
                <xsl:value-of select="csv-xml/record[@line>='1']/csv-field-3"/>
            </accountNo>
            <orderDate>
                <xsl:value-of select="csv-xml/record[@line>='1']/csv-field-5"/>
            </orderDate>
            <orderItems>
                <orderItem>
                    <productCode>
                        <xsl:value-of select="csv-xml/record[@line>='2']/csv-field-3"/>
                    </productCode>
                    <Quantity>
                        <xsl:value-of select="csv-xml/record[@line>='2']/csv-field-4"/>
                    </Quantity>
                </orderItem>
                <orderItem>
                    <productCode>
                        <xsl:value-of select="csv-xml/record[@line>='3']/csv-field-3"/>
                    </productCode>
                    <Quantity>
                        <xsl:value-of select="csv-xml/record[@line>='3']/csv-field-4"/>
                    </Quantity>
                </orderItem>
                <orderItem>
                    <productCode>
                        <xsl:value-of select="csv-xml/record[@line>='4']/csv-field-3"/>
                    </productCode>
                    <Quantity>
                        <xsl:value-of select="csv-xml/record[@line>='4']/csv-field-4"/>
                    </Quantity>
                </orderItem>
                <orderItem>
                    <productCode>
                        <xsl:value-of select="csv-xml/record[@line>='5']/csv-field-3"/>
                    </productCode>
                    <Quantity>
                        <xsl:value-of select="csv-xml/record[@line>='5']/csv-field-4"/>
                    </Quantity>
                </orderItem>
            </orderItems>
        </order>
    </orders>
</xsl:template>

</xsl:stylesheet>

我的输出:

<?xml version="1.0"?>
<orders>
<order>
<accountNo>7654321</accountNo>
<orderDate>08/08/19</orderDate>
<orderItems>
<orderItem>
<productCode>12345678</productCode>
<Quantity>3</Quantity>
</orderItem>
<orderItem>
<productCode>22345679</productCode>
<Quantity>7</Quantity>
</orderItem>
<orderItem>
<productCode>32345680</productCode>
<Quantity>6</Quantity>
</orderItem>
<orderItem>
<productCode>42345681</productCode>
<Quantity>2</Quantity>
</orderItem>
</orderItems>
</order>
</orders>

我想要的输出:

<?xml version="1.0"?>
<orders><order accountNo="7654321" orderDate="08/08/19">
        <orderItems>
            <orderItem productCode="12345678" quantity="3"/>
            <orderItem productCode="22345679" quantity="7"/>
            <orderItem productCode="32345680" quantity="6"/>
            <orderItem productCode="42345681" quantity="2"/>
        </orderItems>
    </order>
</orders>

1 个答案:

答案 0 :(得分:0)

简单地说:

XSLT 1.0

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

<xsl:template match="/csv-xml">
    <xsl:variable name="header" select="record[1]" />
    <orders>
        <order accountNo="{$header/csv-field-3}" orderDate="{$header/csv-field-5}">
            <orderItems>
                <xsl:for-each select="record[position() != 1 and position() != last()]">
                    <orderItem productCode="{csv-field-3}" quantity="{csv-field-4}"/>
                </xsl:for-each>
            </orderItems>
        </order>
    </orders>
</xsl:template>

</xsl:stylesheet>

P.S。了解有关Attribute Value Templates的信息。