尝试使用XSL将XML转换为CSV

时间:2012-03-23 01:24:35

标签: xml xslt csv

我有需要转换为CSV格式的XML数据。我使用应用程序从设计程序生成xml。在这里,我指定了我希望通过自定义XML文件包含在xml中的变量

<?xml version="1.0" encoding="windows-1250"?>
<VarList active="1">
  <Var exclude="0" ID="0" Name="V_Width"/>
  <Var exclude="0" ID="2" Name="V_Depth"/>
  <Var exclude="0" ID="1" Name="V_Height"/>
</VarList>

根据我的自定义XML提供的导出XML数据如下所示

<?xml version="1.0" encoding="utf-16"?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Project.xsd" Name="Job">
  <Version>1</Version>
  <ContentID>1</ContentID>
  <Source>
    <Name>
      <![CDATA[2020 Design]]></Name>
      <BuildVersion>
        <![CDATA[V: 8.1.3072, Build_81_3072]]></BuildVersion>
        <ComputerName>L61795</ComputerName>
      </Source>
      <SendSize>000000000</SendSize>
      <Priority>N</Priority>
      <Design>
        <CheckSum>1092308776</CheckSum>
        <Document>
          <DocType>kitchen</DocType>
          <FilePath>
            <![CDATA[C:\Users\percival_a\Documents\2020 Files\D001_20120323_5.kit]]></FilePath>
            <FileName>
              <![CDATA[D001_20120323_5]]></FileName>
            </Document>
            <JobInfo>
              <FileName>
                <![CDATA[C:\Users\percival_a\Documents\2020 Files\D001_20120323_5.kit]]></FileName>
              </JobInfo>
              <Catalog>
                <Identification>
                  <Name>KITMANIA</Name>
                  <Description>
                    <![CDATA[Kitchen Mania Catalogue]]></Description>
                    <CurrencyName>USD</CurrencyName>
                    <CreationDate>
                      <![CDATA[2011/09/20 00:00:00]]></CreationDate>
                      <ModificationDate>
                        <![CDATA[2012/02/20 14:42:26]]></ModificationDate>
                        <MeasurementUnit>Metric</MeasurementUnit>
                        <Profile>
                          <Name>
                            <![CDATA[Kitchen Mania]]></Name>
                          </Profile>
                          <CatalogHasStyle>0</CatalogHasStyle>
                        </Identification>
                        <Material ID="IDM-1-2605-95005">
                          <Name>White</Name>
                          <Grain>N</Grain>
                          <Thickness>0</Thickness>
                        </Material>
                        <Material ID="IDM-1-2605-8045">
                          <Name>
                            <![CDATA[Metal - Stainless Steel]]></Name>
                            <Grain>N</Grain>
                            <Thickness>0</Thickness>
                          </Material>
                          <FeatureSet ID="IDFS-1" Type="Style">
                            <Code>
                              <![CDATA[1]]></Code>
                              <Description>
                                <![CDATA[Price Level 1]]></Description>
                              </FeatureSet>
                              <Item ID="IDI-1-18">
                                <CheckSum>1092308776</CheckSum>
                                <IsValid>1</IsValid>
                                <UserCode>B400L</UserCode>
                                <LinkCode></LinkCode>
                                <ManufCode>B400</ManufCode>
                                <Description>
                                  <![CDATA[Base cabinet single door ]]></Description>
                                  <Class>111</Class>
                                  <Instance>Standard</Instance>
                                  <ItemTypeName>Cabinets</ItemTypeName>
                                  <ItemType>0</ItemType>
                                  <SubTypeName>Base</SubTypeName>
                                  <SubType>0</SubType>
                                  <ChargeType>0</ChargeType>
                                  <EOType>-1</EOType>
                                  <IsCorner>0</IsCorner>
                                  <IsPlaced>1</IsPlaced>
                                  <IsCustom>0</IsCustom>
                                  <IsWallMounted>0</IsWallMounted>
                                  <IsFloorstanding>1</IsFloorstanding>
                                  <Finish>Both</Finish>
                                  <Hinge>Left</Hinge>
                                  <Quantity>1</Quantity>
                                  <IsForeign>0</IsForeign>
                                  <HasForeign>0</HasForeign>
                                  <Price Type="Cost" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="List" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Retail" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Cost" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="List" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Retail" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Cost" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="List" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Retail" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Cost" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="List" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <Price Type="Retail" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </Price>
                                  <PriceQuantity Type="Cost" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="List" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Retail" Mode="Fixed" Level="1">
                                    <Value>129</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Cost" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="List" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Retail" Mode="Fixed" Level="2">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Cost" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="List" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Retail" Mode="Fixed" Level="3">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Cost" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="List" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <PriceQuantity Type="Retail" Mode="Fixed" Level="4">
                                    <Value>0</Value>
                                    <FormattedValue></FormattedValue>
                                  </PriceQuantity>
                                  <ZoneID>-1</ZoneID>
                                  <AbsolutePos x="164800" y="234800" z="12000"></AbsolutePos>
                                  <Direction>0</Direction>
                                  <NormDir>1</NormDir>
                                  <Var ID="0" Type="Dim">
                                    <Value>400</Value>
                                    <Name>Width</Name>
                                  </Var>
                                  <Var ID="1" Type="Dim">
                                    <Value>720</Value>
                                    <Name>Height</Name>
                                  </Var>
                                  <Var ID="2" Type="Dim">
                                    <Value>562</Value>
                                    <Name>Depth</Name>
                                  </Var>
                                  <FeatureSetRef>IDFS-1</FeatureSetRef>
                                  <LineItemNumber>1</LineItemNumber>
                                </Item>
                                <Pricing>
                                  <GroupTotal Group="Catalog">
                                    <Reference>
                                      <![CDATA[KITMANIA]]></Reference>
                                      <Name/>
                                      <Total>
                                        <BeforeTax>1</BeforeTax>
                                        <Net>0</Net>
                                        <Description>
                                          <![CDATA[KITMANIA total]]></Description>
                                          <Price Type="Retail" Mode="Sum" Level="1">
                                            <Value>129</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="2">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="3">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="4">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                        </Total>
                                        <Total>
                                          <BeforeTax>1</BeforeTax>
                                          <Net>1</Net>
                                          <Description>
                                            <![CDATA[KITMANIA net total]]></Description>
                                            <Price Type="Retail" Mode="Sum" Level="1">
                                              <Value>129</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="2">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="3">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="4">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                          </Total>
                                        </GroupTotal>
                                        <GroupTotal Group="ItemType">
                                          <Reference>Cabinets</Reference>
                                          <GroupReference>0</GroupReference>
                                          <Total>
                                            <BeforeTax>1</BeforeTax>
                                            <Net>0</Net>
                                            <Description>Cabinets total</Description>
                                            <Price Type="Retail" Mode="Sum" Level="1">
                                              <Value>129</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="2">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="3">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="4">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                          </Total>
                                          <Total>
                                            <BeforeTax>1</BeforeTax>
                                            <Net>1</Net>
                                            <Description>Cabinets net total</Description>
                                            <Price Type="Retail" Mode="Sum" Level="1">
                                              <Value>129</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="2">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="3">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="4">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                          </Total>
                                        </GroupTotal>
                                        <GroupTotal Group="ChargeType">
                                          <Reference>Premium</Reference>
                                          <GroupReference>1</GroupReference>
                                          <Total>
                                            <BeforeTax>1</BeforeTax>
                                            <Description>Premium total</Description>
                                            <Price Type="Retail" Mode="Sum" Level="1">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="2">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="3">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="4">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                          </Total>
                                        </GroupTotal>
                                        <GroupTotal Group="ItemSubType">
                                          <Reference>Charges</Reference>
                                          <GroupReference>4</GroupReference>
                                          <Total>
                                            <BeforeTax>1</BeforeTax>
                                            <Description>Charges total</Description>
                                            <Price Type="Retail" Mode="Sum" Level="1">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="2">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="3">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                            <Price Type="Retail" Mode="Sum" Level="4">
                                              <Value>0</Value>
                                              <FormattedValue></FormattedValue>
                                            </Price>
                                          </Total>
                                        </GroupTotal>
                                      </Pricing>
                                      <TotalWeight>0</TotalWeight>
                                      <TotalVolume>0</TotalVolume>
                                    </Catalog>
                                    <Pricing>
                                      <CurrencyCode></CurrencyCode>
                                      <GroupTotal Group="Design">
                                        <Total>
                                          <BeforeTax>1</BeforeTax>
                                          <Net>0</Net>
                                          <Description>Design total</Description>
                                          <Price Type="Retail" Mode="Sum" Level="1">
                                            <Value>129</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="2">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="3">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="4">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                        </Total>
                                        <Total>
                                          <BeforeTax>0</BeforeTax>
                                          <Net>1</Net>
                                          <Description>Design net total</Description>
                                          <Price Type="Retail" Mode="Sum" Level="1">
                                            <Value>129</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="2">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="3">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                          <Price Type="Retail" Mode="Sum" Level="4">
                                            <Value>0</Value>
                                            <FormattedValue></FormattedValue>
                                          </Price>
                                        </Total>
                                      </GroupTotal>
                                    </Pricing>
                                  </Design>
                                </Project>

我刚刚编辑了我的问题,对此我很抱歉。然而,感兴趣的领域是(Var内容):

<Var ID="0" Type="Dim">
    <Value>400</Value>
    <Name>Width</Name>
</Var>
<Var ID="1" Type="Dim">
    <Value>720</Value>
    <Name>Height</Name>
</Var>
<Var ID="2" Type="Dim">
    <Value>562</Value>
    <Name>Depth</Name>
</Var>

这是我想要一个XSL(可以在应用程序中指定)将其转换为CSV格式的地方:

Width   Height  Depth
400 720 562

我可以使用多个XSL在CSV格式之前进入备用XML格式 - 因为我一直在关注stackoverflow上的其他Q&amp; A;并怀疑事先可能需要这样做。

过去曾使用过XmlToCsvConverter,其中CSV的格式需要引起注意。

感谢任何帮助,因为我只是在寻找自己的方式 - 并且怀疑专家程序员这将是在公园散步。亲切的问候

1 个答案:

答案 0 :(得分:3)

虽然有点不清楚具体要求是什么,或者这是最好的方法,但这里有一段XSLT可以将上面显示的XML转换为单行CSV,就像输出示例一样。如果没有更完整的样本,例如多行,则难以概括解决方案(同样,您的样本导出的XML数据也会因为缺少根元素而导致格式错误):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
  <xsl:for-each select="//Var">
    <xsl:value-of select="Name" /><xsl:text> </xsl:text>
  </xsl:for-each><xsl:text>
</xsl:text>
  <xsl:for-each select="//Var">
    <xsl:value-of select="Value" /><xsl:text> </xsl:text>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

以下是我用来测试的工具:http://xslttest.appspot.com/,使用以下略微修改的XML数据(添加了根元素):

<Vars>
    <Var ID="0" Type="Dim">
        <Value>400</Value>
        <Name>Width</Name>
    </Var>
    <Var ID="1" Type="Dim">
        <Value>720</Value>
        <Name>Height</Name>
    </Var>
    <Var ID="2" Type="Dim">
        <Value>562</Value>
        <Name>Depth</Name>
    </Var>
</Vars>