【发布时间】:2017-04-28 00:14:34
【问题描述】:
<?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 中的样子(开始,成功后需要做一些计算):
在这个例子中,在一所房子 (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
谢谢。
【问题讨论】:
-
家具节点是否会在没有最小或最大限制的房间内变化?
-
是的。但每个房间只有 1 个数据,即每件家具价格的总和..即一个房间有 4 件家具,csv 中的输出将是这 4 件家具的总金额..
-
你的底部结果总和比你原来的要容易甚至。顶部的逗号分隔值是什么?等等...为什么
<a>总和是 40000 而不是 30000,接下来是 70000,接下来是 110000? -
@Parfait
sum(@FURN_AMT * @price),即a = 2 * 10000 + 1 * 20000 = 40000。 -
f的结果是590000,而不是350000。
标签: java xml csv xslt text-files