java - 通过 XSLT 将异常 XML 数据转换为 CSV

标签 java xml csv xslt text-files

<?xml version="1.0" encoding="UTF-8"?>
<FirstTag version="1.0" createTime="15:59:59" DATE="20161209">
  <SecondTag Name="House01">
    <a>
        <Furniture FURN_ID="FUR00001" FURN_AMT="2" price="10000"/>
        <Furniture FURN_ID="FUR00002" FURN_AMT="1" price="20000"/>
    </a>
    <b>
        <Furniture FURN_ID="FUR00003" FURN_AMT="2" price="30000"/>
        <Furniture FURN_ID="FUR00004" FURN_AMT="1" price="40000"/>
    </b>
    <c>
        <Furniture FURN_ID="FUR00005" FURN_AMT="2" price="50000"/>
        <Furniture FURN_ID="FUR00006" FURN_AMT="1" price="60000"/>
    </c>
    <d>
        <Furniture FURN_ID="FUR00007" FURN_AMT="1" price="70000"/>
        <Furniture FURN_ID="FUR00008" FURN_AMT="1" price="80000"/>
    </d>
    <e>
        <Furniture FURN_ID="FUR00009" FURN_AMT="1" price="90000"/>
        <Furniture FURN_ID="FUR00010" FURN_AMT="1" price="100000"/>
    </e>
    <f>
        <Furniture FURN_ID="FUR00011" FURN_AMT="1" price="110000"/>
        <Furniture FURN_ID="FUR00012" FURN_AMT="2" price="120000"/>
        <Furniture FURN_ID="FUR00013" FURN_AMT="2" price="120000"/>
    </f>
  </SecondTag>
</FirstTag>

上面是我从 Java 程序生成的简单 xml(带有节点值)。关键是,我想将此 xml 数据发送到另一个应用程序,其中已经有来自 UI/批处理的 csv 加载函数。我听说过 XSLT 但从未使用过它,尝试了一些教程但在将所有值放入 csv 时感到困惑。

这是它在 csv 中的样子(开始,成功后需要做一些计算):

sample row csv

在这个例子中,在一个房子 (HOUSE01) 中,我想输出不同房间的所有家具(即 a 是房间 1,b 是房间 2,c 是房间 3,等等)。

我一直在尝试构建 XSLT,下面是 XSLT:

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

  <xsl:variable name="delimiter" select="','" />

  <!-- define an array containing the fields we are interested in -->
  <xsl:variable name="fieldArray">
    <field>Name</field>
    <field>a</field>
    <field>b</field>
    <field>c</field>
    <field>d</field>
    <field>e</field>
    <field>f</field>
  </xsl:variable>
  <xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" />

  <xsl:template match="/">

    <!-- output the header row -->
    <xsl:for-each select="$fields">
      <xsl:if test="position() != 1">
        <xsl:value-of select="$delimiter"/>
      </xsl:if>
      <xsl:value-of select="." />
    </xsl:for-each>

    <!-- output newline -->
    <xsl:text>
</xsl:text>

    <xsl:apply-templates select="/*/*"/>
  </xsl:template>

  <xsl:template match="a">
    <xsl:variable name="currNode" select="." />

<!--     output the data row -->
<!--     loop over the field names and find the value of each one in the xml -->
    <xsl:for-each select="$fields">
      <xsl:if test="position() != 1">
        <xsl:value-of select="$delimiter"/>
      </xsl:if>
      <xsl:value-of select="$currNode/*[name() = current()]/@FURN_ID" />
<!--       <xsl:value-of select="$currNode/*[name() = current()]" /> -->
    </xsl:for-each>

<!--     output newline -->
    <xsl:text>
</xsl:text>
  </xsl:template>
</xsl:stylesheet>

我正在使用其他页面的一些引用,并且可以构建一些简单的 XSLT 以将 XML 转换为 CSV,但是,我需要一些指导才能解决我的主要 XML 问题。将来我可以在循环中获取节点值后,我想对每个房间的每件家具的总价求和。

预期的最终 csv 结果:

Name,a,b,c,d,e,f
House01,40000,100000,160000,150000,190000,350000

谢谢。

Getting the value of an attribute in XML

最佳答案

此 XSLT 将提供您指定的输出。参见 demo .

更新:我错过了输出中的 a 值。

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

  <xsl:template match="/">
    <xsl:text>Name,a,b,c,d,e,f
</xsl:text>
    <xsl:apply-templates select="FirstTag/SecondTag/a/Furniture"/>
  </xsl:template>

  <xsl:template match="Furniture">
    <xsl:variable name="pos" select="position()"/>
    <xsl:value-of select="../../@Name"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="@FURN_ID"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="../../b/Furniture[position()=$pos]/@FURN_ID"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="../../c/Furniture[position()=$pos]/@FURN_ID"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="../../d/Furniture[position()=$pos]/@FURN_ID"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="../../e/Furniture[position()=$pos]/@FURN_ID"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="../../f/Furniture[position()=$pos]/@FURN_ID"/>
    <xsl:text>
</xsl:text>
  </xsl:template>

</xsl:stylesheet>

关于java - 通过 XSLT 将异常 XML 数据转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41112562/

相关文章:

java - Xml 1.1 与 1.0 库的使用

xml - 从 Flash 中的 swf 文件外部加载 xml 文件

mysql - 可湿性粉剂 : import custom fields for taxonomy term

java - Spring Boot ErrorController 无法与 OpenShift 一起使用

xml - POX ("plain old XML") 的意义是什么?

java - 如何获取匹配请求的字段?

c# - 如何创建 schema.ini 文件?我需要将我的 .csv 文件导出到 datagridview

python - 如何合并两个csv文件?

java - 我的java代码有缺陷,但我不明白为什么

java - 在 DAO 接口(interface)方法中声明或不声明 DataAccessException?