2
我目前正在尝试展平一个xml结构以显示在一个简单的表中。 基本的问题是,xml包含不同级别上的重复节点 - 提取节点的每个组合都应该导致一个单独的输出节点。使用XSLT展平XML树 - 可选但重复节点
XML文档看起来是这样的:
<customer>
<name>Mustermann</name>
<contract>
<contract_id>C1</contract_id>
<products>
<product>
<name>Product C1.P1</name>
<price>23.12</price>
<properties>
<property>Property C1.P1.A</property>
<property>Property C1.P1.B</property>
</properties>
</product>
<product>
<name>Product C1.P2</name>
<price>2.32</price>
</product>
</products>
</contract>
<contract>
<contract_id>C2</contract_id>
<products>
<product>
<name>Product C2.P1</name>
<price>143.33</price>
</product>
<product>
<name>Product C2.P2</name>
<price>231.76</price>
<properties>
<property>Property C2.P2.A</property>
<property>Property C2.P2.B</property>
</properties>
</product>
</products>
</contract>
<contract>
<contract_id>C3</contract_id>
</contract>
</customer>
所以合同并不需要有产品,产品并不需要有属性。
结果应该使重复节点变平并为每组提取的节点形成一个单独的结果节点(这当然会在结果中引入一些冗余)。
输出应该是这样的:
<output>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C1</contract_id>
<product_name>Product C1.P1</product_name>
<product_price>23.12</product_price>
<product_property>Property C1.P1.A</product_property>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C1</contract_id>
<product_name>Product C1.P1</product_name>
<product_price>23.12</product_price>
<product_property>Property C1.P1.B</product_property>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C1</contract_id>
<product_name>Product C1.P2</product_name>
<product_price>2.32</product_price>
<product_property/>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C2</contract_id>
<product_name>Product C2.P1</product_name>
<product_price>143.33</product_price>
<product_property/>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C2</contract_id>
<product_name>Product C2.P2</product_name>
<product_price>231.76</product_price>
<product_property>Property C2.P2.A</product_property>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C2</contract_id>
<product_name>Product C2.P2</product_name>
<product_price>231.76</product_price>
<product_property>Property C2.P2.B</product_property>
</data>
<data>
<customer_name>Mustermann</customer_name>
<contract_id>C3</contract_id>
<product_name/>
<product_price/>
<product_property/>
</data>
</output>
我尝试以下XSLT样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/customer">
<output>
<xsl:for-each select="contract">
<xsl:choose>
<xsl:when test="products/product">
<xsl:for-each select="products/product">
<xsl:choose>
<xsl:when test="properties/property">
<xsl:for-each select="properties/property">
<xsl:call-template name="data">
<xsl:with-param name="customer_name" select="/customer/name"/>
<xsl:with-param name="contract_id" select="../../../../contract_id"/>
<xsl:with-param name="product_name" select="../../name"/>
<xsl:with-param name="product_price" select="../../price"/>
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="data">
<xsl:with-param name="customer_name" select="/customer/name"/>
<xsl:with-param name="contract_id" select="../../contract_id"/>
<xsl:with-param name="product_name" select="name"/>
<xsl:with-param name="product_price" select="price"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="data">
<xsl:with-param name="customer_name" select="/customer/name"/>
<xsl:with-param name="contract_id" select="contract_id"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</output>
</xsl:template>
<xsl:template name="data">
<xsl:param name="customer_name"/>
<xsl:param name="contract_id"/>
<xsl:param name="product_name"/>
<xsl:param name="product_price"/>
<xsl:param name="property"/>
<data>
<customer_name><xsl:value-of select="$customer_name"/></customer_name>
<contract_id><xsl:value-of select="$contract_id"/></contract_id>
<product_name><xsl:value-of select="$product_name"/></product_name>
<product_price><xsl:value-of select="$product_price"/></product_price>
<product_property><xsl:value-of select="$property"/></product_property>
</data>
</xsl:template>
</xsl:stylesheet>
这将产生期望的结果,但对我来说,这看起来相当丑陋。 对于只执行嵌套循环(合同上的循环,循环内部产品等等)的朴素方法不起作用,因为它没有选取所有输出节点(例如没有产品的合同被忽略)。
背景:我想在oracle xmltable查询中使用xslt样式表,并将中间结果映射到简单的行/列输出。因此,每个结果都需要位于其自己的行中。
任何帮助表示赞赏!
谢谢您的回答!这样更优雅!一些背景:我们在oracle数据库中有xml-data,用户可以根据自己指定的xpath-queries来定义报告。 XML数据的结构如我的问题所示,结果应该基于表格。所以是的,结果是有用的:) – 2015-02-12 08:14:32
更多背景:报告可能是“获取所有合同ID,如果存在,产品的ID”。用户不知道xml-data中存在哪些节点,并且被限制为指定xpath-expressions。 – 2015-02-12 08:22:54