xslt 1.0中每个订单的条件序列号

时间:2014-01-27 07:18:09

标签: xslt-1.0

如何使用条件生成序列号。

下面是输入,在此<OrderLines>中是<LineValue1>内的无界元素。如果<LineValue1>为NULL,则我们必须忽略此<LineValue1>元素并且不增加序列号。与<Item>相同的方式是<OrderLines>的子元素。如果{{ 1}}是Null然后我们必须忽略这个元素并且不增加序列号。否则我们必须增加序列号。如下面的输出。

输入:

<ItemValue1>

输出:

<?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>

我尝试了下面的xslt它几乎是wrking,但问题并没有忽略上述情况下的序列号。

XSLT:

<?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>

1 个答案:

答案 0 :(得分:0)

尝试使用此逻辑(我只是说逻辑,因为我不确定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>