如何生成带条件的序列号。
下面是输入,在这个<OrderLines>
里面是无界元素,这个<LineValue1>
就在那里。如果<LineValue1>
为NULL,那么我们必须忽略这个<LineValue1>
元素,并且不递增序列号。与<Item>
相同的方法是<OrderLines>
的子元素。如果<ItemValue1>
为Null,则我们必须忽略此元素,并且不递增序号。否则,我们必须递增序号。如低于输出。
输入:
<?xml version="1.0" encoding="UTF-8" ?>
<process xmlns="http://xmlns.oracle.com/JMS_TEST/JMS_Publish/JMSSendToQueue">
<OrderHeader><!-- unbounded -->
<HeaderValue1>HeaderValue11063</HeaderValue1>
<OrderLines><!-- unbounded -->
<LineValue1>LineValue11064</LineValue1>
<Linelevel>Linelevel1419</Linelevel>
<Linesyb>Linesyb1420</Linesyb>
<Item><!-- unbounded -->
<ItemValue1></ItemValue1><!-- if ItemValue1 is null then ignore the sequence number(don't increment) -->
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1><!-- unbounded -->
<ElementValue1>ElementValue11070</ElementValue1>
<ElementLevel>ElementLevel1428</ElementLevel>
<Elementsyb>Elementsyb1429</Elementsyb>
</Element1>
</Item>
<Item><!-- unbounded -->
<ItemValue1>Itemvalue123</ItemValue1>
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1><!-- unbounded -->
<ElementValue1>ElementValue11070</ElementValue1>
<ElementLevel>ElementLevel1428</ElementLevel>
<Elementsyb>Elementsyb1429</Elementsyb>
</Element1>
</Item>
</OrderLines>
<OrderLines>
<LineValue1></LineValue1><!-- if LineValue1 is null then ignore the sequence number(don't increment) -->
<Linelevel>Linelevel1419</Linelevel>
<Linesyb>Linesyb1420</Linesyb>
<Item>
<ItemValue1>ItemValue11067</ItemValue1>
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1>
<ElementValue1>ElementValue11070</ElementValue1>
<ElementLevel>ElementLevel1425</ElementLevel>
<Elementsyb>Elementsyb1426</Elementsyb>
</Element1>
</Item>
</OrderLines>
</OrderHeader>
</process>
输出:
<?xml version = '1.0' encoding = 'UTF-8'?>
<process xmlns:ns1="http://xmlns.oracle.com/JMS_TEST/JMS_Publish/JMSSendToQueue">
<OrderHeader>
<HeaderValue1>1</HeaderValue1><!--sequence number starts from 1 -->
<OrderLines>
<LineValue1>2</LineValue1><!-- The LineValue1 is NOT NULL so increment the sequence number by '1'-->
<Linelevel>Linelevel1419</Linelevel>
<Linesyb>Linesyb1420</Linesyb>
<Item><!-- The ItemValue1 is NULL so don't increment the sequence number by '1' and ignore the ItemValue1-->
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1>
<ElementValue1>3</ElementValue1><!-- The ElementValue1 is NOT NULL so increment the sequence number by '1'-->
<ElementLevel>ElementLevel1428</ElementLevel>
<Elementsyb>Elementsyb1429</Elementsyb>
</Element1>
</Item>
<Item>
<ItemValue1>4</ItemValue1><!-- The ItemValue1 is NOT NULL so increment the sequence number by '1'-->
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1>
<ElementValue1>5</ElementValue1><!-- The ElementValue1 is NOT NULL so increment the sequence number by '1'-->
<ElementLevel>ElementLevel1428</ElementLevel>
<Elementsyb>Elementsyb1429</Elementsyb>
</Element1>
</Item>
</OrderLines>
<OrderLines>
<Item>
<ItemValue1>6</ItemValue1>
<ItemLevel>ItemLevel1422</ItemLevel>
<Itemsyb>Itemsyb1423</Itemsyb>
<Element1>
<ElementValue1>7</ElementValue1>
<ElementLevel>ElementLevel1428</ElementLevel>
<Elementsyb>Elementsyb1429</Elementsyb>
</Element1>
</Item>
</OrderLines>
</OrderHeader>
</process>
我尝试了下面的xslt,它几乎是wrking,但问题不是在上述情况下忽略序列号。
xslt:
<xsl:template match="/">
<process>
<xsl:for-each select="/process/OrderHeader">
<xsl:variable name="headerPosition">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:variable name="headerVar">
<xsl:value-of select="
int(
count(/process/OrderHeader[position()<$headerPosition]//OrderLines)+
count(/process/OrderHeader[position()<$headerPosition]//OrderLines//Item)+
count(/process/OrderHeader[position()<$headerPosition]//OrderLines//Element1)+
position())"/>
</xsl:variable>
<OrderHeader>
<HeaderValue1>
<xsl:value-of select="$headerVar"/>
</HeaderValue1>
<xsl:for-each select="ns1:OrderLines">
<xsl:variable name="LinePos">
<xsl:value-of select="int(position())"/>
</xsl:variable>
<xsl:variable name="LineVar">
<xsl:value-of select="int(
$headerVar+
count(/process/OrderHeader[position()=$headerPosition]/OrderLines[position()<$LinePos]//Item)+
count(/process/OrderHeader[position()=$headerPosition]/OrderLines[position()<$LinePos]//Item//Element1)+
position())"/>
</xsl:variable>
<OrderLines>
<xsl:if test="LineValue1!=''">
<LineValue1>
<xsl:value-of select="$LineVar"/>
</LineValue1>
</xsl:if>
<xsl:for-each select="ns1:Item">
<xsl:variable name="ItemPos">
<xsl:value-of select="int(position())"/>
</xsl:variable>
<xsl:variable name="ItemVar">
<xsl:value-of select="int(
$LineVar+
count(/process/OrderHeader[position()=$headerPosition]/OrderLines[position()=$LinePos]//Item[position()<$ItemPos]//Element1)+
position())"/>
</xsl:variable>
<Item>
<xsl:if test="ItemValue1!=''">
<ItemValue1>
<xsl:value-of select="$ItemVar"/>
</ItemValue1>
</xsl:if>
<xsl:for-each select="Element1">
<Element1>
<ElementValue1>
<xsl:value-of select="$ItemVar+position()"/>
</ElementValue1>
</Element1>
</xsl:for-each>
</Item>
</xsl:for-each>
</OrderLines>
</xsl:for-each>
</OrderHeader>
</xsl:for-each>
</process>
</xsl:template>
试试这个逻辑(我说逻辑只是因为我不确定XML文件中存在的名称空间)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="process/OrderHeader/HeaderValue1">
<xsl:copy>
<xsl:value-of select="count(.) + count(preceding::HeaderValue1[.!=''])"/>
</xsl:copy>
</xsl:template>
<xsl:template match="process/OrderHeader/OrderLines/LineValue1">
<xsl:if test=". != ''">
<LineValue1>
<xsl:value-of select="count(.) + count(preceding::LineValue1[.!='']) + count(preceding::HeaderValue1[.!=''])"/>
</LineValue1>
</xsl:if>
</xsl:template>
<xsl:template match="process/OrderHeader/OrderLines/Item/ItemValue1">
<xsl:if test=".!=''">
<ItemValue1>
<xsl:value-of select="count(.) + count(preceding::ElementValue1[.!='']) + count(preceding::ItemValue1[.!='']) + count(preceding::LineValue1[.!='']) + count(preceding::HeaderValue1[.!=''])"/>
</ItemValue1>
</xsl:if>
</xsl:template>
<xsl:template match="process/OrderHeader/OrderLines/Item/Element1/ElementValue1">
<ElementValue1>
<xsl:value-of select="count(.) + count(preceding::ElementValue1[.!='']) + count(preceding::ItemValue1[.!='']) + count(preceding::LineValue1[.!='']) + count(preceding::HeaderValue1[.!=''])"/>
</ElementValue1>
</xsl:template>
</xsl:stylesheet>