在货件中,订单数量存在。对于每个订单,都有许多订单行项目。订单行项目包含项目。对于每次迭代,我们都在维护层次结构级别。在orderLine Items部分中,如果前一项等于当前项,则无需增加层次结构。
分:
对于订单和orderlineItems的每次迭代,我们必须维护层次结构级别(增量值),但是当到达orderLineItem时,如果前一项等于当前项,则存在一个条件,不需要增加层次结构级别另外,我们应该增加层次结构级别,如下面我们需要的输出。
以下是输入。
输入:
<shipment>
<order><!-- unbounded -->
<value>value 4</value>
<other>item1</other>
<OrderLineItems><!-- unbounded -->(conditions comes under this section only not in Order section)
<Lineitem>3</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>3</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>2</LineItem>
</OrderLineItems>
</order>
<order>
<value>value 98</value>
<other>item2</other>
<OrderLineItems>
<Lineitem>8</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>3</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>4</LineItem>
</OrderLineItems>
</order>
<order>
<value>value 98</value>
<other>item2</other>
<OrderLineItems>
<Lineitem>8</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>3</LineItem>
</OrderLineItems>
<OrderLineItems>
<Lineitem>4</LineItem>
</OrderLineItems>
</order>
</shipment>
以下是我们需要的输出。
输出:
<ns1:Shipment>
<ns1:Shipment1>1</ns1:Shipment1>
<ns1:ShipmentValue>S</ns1:ShipmentValue>
<ns1:Order>
<ns1:Orderlevel1>2</ns1:Orderlevel1>
<ns1:Orderlevel2>1</ns1:Orderlevel2>
<ns1:Orderlevel3>O</ns1:Orderlevel3>
<ns1:OrderLineItems>
<ns1:Level1>3</ns1:Level1>
<ns1:Level2>2</ns1:Level2>
<ns1:Level3>I</ns1:Level3>
<ns1:Item>Item59</ns1:Item>
</ns1:OrderLineItems>
<ns1:OrderLineItems>
<ns1:Item>Item59</ns1:Item>
</ns1:OrderLineItems>
<ns1:OrderLineItems>
<ns1:Level1>4</ns1:Level1>
<ns1:Level2>2</ns1:Level2>
<ns1:Level3>I</ns1:Level3>
<ns1:Item>Item63</ns1:Item>
</ns1:OrderLineItems>
</ns1:Order>
<ns1:Order>
<ns1:Orderlevel1>5</ns1:Orderlevel1>
<ns1:Orderlevel2>1</ns1:Orderlevel2>
<ns1:Orderlevel3>O</ns1:Orderlevel3>
<ns1:OrderLineItems>
<ns1:Level1>6</ns1:Level1>
<ns1:Level2>5</ns1:Level2>
<ns1:Level3>I</ns1:Level3>
<Item>Item74</Item>
</ns1:OrderLineItems>
<ns1:OrderLineItems>
<ns1:Level1>7</ns1:Level1>
<ns1:Level2>5</ns1:Level2>
<ns1:Level3>I</ns1:Level3>
<Item>Item78</Item>
</ns1:OrderLineItems>
<ns1:OrderLineItems>
<ns1:Item>Item78</ns1:Item>
</ns1:OrderLineItems>
</ns1:Order>
</ns1:Shipment>
答案 0 :(得分:0)
我已经简化了你的问题(假设我理解正确,我不确定)。
无论如何,对于这个XML:
<shipment>
<order>
<value>value 4</value>
<other>item1</other>
</order>
<order>
<value>value 98</value>
<other>item2</other>
</order>
<order>
<value>value 98</value>
<other>item3</other>
</order>
<order>
<value>another value</value>
<other>item4</other>
</order>
</shipment>
我收到以下输出(注意<counter>
每次<value>
正在变化时递增的元素):
<shipment>
<order>
<counter>1</counter>
<value>value 4</value>
<other>item1</other>
</order>
<order>
<!-- counter incremented, because value changed
from "value 4" to "value 98" -->
<counter>2</counter>
<value>value 98</value>
<other>item2</other>
</order>
<order>
<!-- counter not incremented, value still "value 98" -->
<counter>2</counter>
<value>value 98</value>
<other>item3</other>
</order>
<order>
<!-- counter incremented, because value changed
from "value 98" to "another value" -->
<counter>3</counter>
<value>another value</value>
<other>item4</other>
</order>
</shipment>
如果这对您有用,请查看此样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<shipment>
<!-- call template for first <order> only. All subsequent <order>
elements will be called recursively by this template itself -->
<xsl:call-template name="order">
<xsl:with-param name="prevValue" select="string('')"/>
<xsl:with-param name="counter" select="0"/>
<xsl:with-param name="self" select="/shipment/order[1]"/>
</xsl:call-template>
</shipment>
</xsl:template>
<xsl:template name="order">
<xsl:param name="prevValue"/>
<xsl:param name="counter"/>
<xsl:param name="self"/>
<xsl:variable name="selfCounter">
<xsl:choose>
<!-- if the <value> is the same as in previous <order>,
do not increment <counter> -->
<xsl:when test="$prevValue = $self/value">
<xsl:value-of select="$counter"/>
</xsl:when>
<!-- if the value is different, increment <counter> by one -->
<xsl:otherwise>
<xsl:value-of select="$counter + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<order>
<!-- copy all the original children of <order>,
and add <counter> -->
<counter><xsl:value-of select="$selfCounter"/></counter>
<xsl:copy-of select="$self/node()" />
</order>
<!-- if there is another <order>, call this template -->
<xsl:if test="$self/following-sibling::order[1]">
<xsl:call-template name="order">
<xsl:with-param name="prevValue" select="$self/value"/>
<xsl:with-param name="counter" select="$selfCounter"/>
<xsl:with-param name="self" select="$self/following-sibling::order[1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>