3

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Item>
    <RECORD_ID>RECORD_ID</RECORD_ID>
    <ENTITY_CODE>ENTITY_CODE</ENTITY_CODE>
    <USER_CODE>USER_CODE</USER_CODE>
    <RECORD_DATE>RECORD_DATE</RECORD_DATE>
    <ITEM_CODE>ITEM_CODE</ITEM_CODE>
    <LINE_QUANTITY>LINE_QUANTITY</LINE_QUANTITY>
    <LINE_FREE_STOCK>LINE_FREE STOCK</LINE_FREE_STOCK>
    <LINE_PRICE>LINE_PRICE</LINE_PRICE>
    <LINE_DISCOUNT_PERCENT>LINE_DISCOUNT PERCENT</LINE_DISCOUNT_PERCENT>
</Item>
<Item>
    <RECORD_ID>9046</RECORD_ID>
    <ENTITY_CODE>12010601</ENTITY_CODE>
    <USER_CODE>122</USER_CODE>
    <RECORD_DATE>2011-08-24</RECORD_DATE>
    <ITEM_CODE>804-008165</ITEM_CODE>
    <LINE_QUANTITY>2</LINE_QUANTITY>
    <LINE_FREE_STOCK>1</LINE_FREE_STOCK>
</Item>
<Item>
    <RECORD_ID>9046</RECORD_ID>
    <ENTITY_CODE>12010601</ENTITY_CODE>
    <USER_CODE>122</USER_CODE>
    <RECORD_DATE>2011-08-24</RECORD_DATE>
    <ITEM_CODE>804-008161</ITEM_CODE>
    <LINE_QUANTITY>1</LINE_QUANTITY>
    <LINE_FREE_STOCK>1</LINE_FREE_STOCK>
</Item>
<Item>
    <RECORD_ID>9046</RECORD_ID>
    <ENTITY_CODE>12010601</ENTITY_CODE>
    <USER_CODE>122</USER_CODE>
    <RECORD_DATE>2011-08-24</RECORD_DATE>
    <ITEM_CODE>804-008225</ITEM_CODE>
    <LINE_QUANTITY>5</LINE_QUANTITY>
</Item>
</Order>

有时在item标签内我有 element <LINE_FREE_STOCK>。如果发生这种情况,我必须在输出 XML 中创建一个额外的位置。

现在我想出了这个样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>

<xsl:template match="/">
    <ORDERS05>
        <IDOC BEGIN="1">
            <xsl:apply-templates select="Order"/>
        </IDOC>
    </ORDERS05>
</xsl:template>

<xsl:template match="Order">
    <Header>
        <xsl:value-of select="'some header data'"/>
    </Header>
    <xsl:apply-templates select="Item[position() >1]"/>
    <xsl:apply-templates select="Item[position() >1 and child::LINE_FREE_STOCK]" mode="freestock"/>
</xsl:template>

<xsl:template match="Item">
        <position>
            <item>
                <number><xsl:value-of select="ITEM_CODE"/></number>
                <quantity><xsl:value-of select="LINE_QUANTITY"/></quantity>
            </item>
        </position>
</xsl:template>

<xsl:template match="Item[position() >1 and child::LINE_FREE_STOCK]" mode="freestock">
        <position>
            <item>
                <number><xsl:value-of select="ITEM_CODE"/></number>
                <freestock_quant><xsl:value-of select="LINE_FREE_STOCK"/></freestock_quant>
            </item>
        </position>
</xsl:template>

</xsl:stylesheet>

它创建了这个(简化的)想要的输出:

<?xml version="1.0" encoding="UTF-8"?>
<ORDERS05>
<IDOC BEGIN="1">
    <Header>some header data</Header>
    <position>
        <item>
            <number>804-008165</number>
            <quantity>2</quantity>
        </item>
    </position>
    <position>
        <item>
            <number>804-008161</number>
            <quantity>1</quantity>
        </item>
    </position>
    <position>
        <item>
            <number>804-008225</number>
            <quantity>5</quantity>
        </item>
    </position>
    <position>
        <item>
            <number>804-008165</number>
            <freestock_quant>1</freestock_quant>
        </item>
    </position>
    <position>
        <item>
            <number>804-008161</number>
            <freestock_quant>1</freestock_quant>
        </item>
    </position>
</IDOC>
</ORDERS05>

804-008165 和 804-008161 出现两次 - 一次作为标准项目,一次作为具有相应数量的免费库存项目。

但是我在这里忘记了什么吗?有什么我看不到的陷阱吗?XSLT 足够健壮吗?

4

4 回答 4

4

正如其他人所指出的,问题出在这段代码中

<xsl:apply-templates select="Item"/> 
<xsl:apply-templates select="Item[child::LINE_FREE_STOCK]" mode="freestock"/> 

如果有一个 childItem有一个 child LINE_FREE_STOCK,则模板将应用于此Item元素两次——这是在输出中获得重复的方式。

转换可以显着缩短,并且根本不需要模式或显式条件指令

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output encoding="UTF-8" indent="yes"/>

 <xsl:template match="/">
    <ORDERS05>
        <IDOC BEGIN="1">
            <xsl:apply-templates select="Order"/>
        </IDOC>
    </ORDERS05>
 </xsl:template>

 <xsl:template match="Order">
    <Header>
        <xsl:value-of select="'some header data'"/>
    </Header>
        <xsl:apply-templates select="Item[position() >1]"/>
 </xsl:template>

 <xsl:template match="Item">
  <position>
    <item>
      <number>
        <xsl:value-of select="ITEM_CODE"/>
      </number>

      <xsl:apply-templates select=
        "self::node()[not(LINE_FREE_STOCK)]/LINE_QUANTITY
       |
         LINE_FREE_STOCK"/>
     </item>
  </position>
 </xsl:template>

 <xsl:template match="LINE_QUANTITY">
   <quantity>
     <xsl:value-of select="."/>
   </quantity>
 </xsl:template>

 <xsl:template match="LINE_FREE_STOCK">
   <freestock_quant>
     <xsl:value-of select="."/>
   </freestock_quant>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<Order>
    <Item>
        <RECORD_ID>RECORD_ID</RECORD_ID>
        <ENTITY_CODE>ENTITY_CODE</ENTITY_CODE>
        <USER_CODE>USER_CODE</USER_CODE>
        <RECORD_DATE>RECORD_DATE</RECORD_DATE>
        <ITEM_CODE>ITEM_CODE</ITEM_CODE>
        <LINE_QUANTITY>LINE_QUANTITY</LINE_QUANTITY>
        <LINE_FREE_STOCK>LINE_FREE STOCK</LINE_FREE_STOCK>
        <LINE_PRICE>LINE_PRICE</LINE_PRICE>
        <LINE_DISCOUNT_PERCENT>LINE_DISCOUNT PERCENT</LINE_DISCOUNT_PERCENT>
    </Item>
    <Item>
        <RECORD_ID>9046</RECORD_ID>
        <ENTITY_CODE>12010601</ENTITY_CODE>
        <USER_CODE>122</USER_CODE>
        <RECORD_DATE>2011-08-24</RECORD_DATE>
        <ITEM_CODE>804-008165</ITEM_CODE>
        <LINE_QUANTITY>2</LINE_QUANTITY>
        <LINE_FREE_STOCK>1</LINE_FREE_STOCK>
    </Item>
    <Item>
        <RECORD_ID>9046</RECORD_ID>
        <ENTITY_CODE>12010601</ENTITY_CODE>
        <USER_CODE>122</USER_CODE>
        <RECORD_DATE>2011-08-24</RECORD_DATE>
        <ITEM_CODE>804-008161</ITEM_CODE>
        <LINE_QUANTITY>1</LINE_QUANTITY>
        <LINE_FREE_STOCK>1</LINE_FREE_STOCK>
    </Item>
    <Item>
        <RECORD_ID>9046</RECORD_ID>
        <ENTITY_CODE>12010601</ENTITY_CODE>
        <USER_CODE>122</USER_CODE>
        <RECORD_DATE>2011-08-24</RECORD_DATE>
        <ITEM_CODE>804-008225</ITEM_CODE>
        <LINE_QUANTITY>5</LINE_QUANTITY>
    </Item>
</Order>

产生了想要的正确结果

<ORDERS05>
   <IDOC BEGIN="1">
      <Header>some header data</Header>
      <position>
         <item>
            <number>804-008165</number>
            <freestock_quant>1</freestock_quant>
         </item>
      </position>
      <position>
         <item>
            <number>804-008161</number>
            <freestock_quant>1</freestock_quant>
         </item>
      </position>
      <position>
         <item>
            <number>804-008225</number>
            <quantity>5</quantity>
         </item>
      </position>
   </IDOC>
</ORDERS05>
于 2011-08-26T03:05:37.887 回答
1

这是因为您有两个 Item 匹配模板:

<xsl:template match="Item">
  <xsl:if test="position() &gt; 1">
    <position>
      <item>
        <number><xsl:value-of select="ITEM_CODE"/></number>
        <quantity><xsl:value-of select="LINE_QUANTITY"/></quantity>
      </item>
    </position>
  </xsl:if>
</xsl:template>

<xsl:template match="Item[child::LINE_FREE_STOCK]" mode="freestock">
  <xsl:if test="position() &gt; 1">            
    <position>
      <item>
        <number><xsl:value-of select="ITEM_CODE"/></number>
        <freestock_quant><xsl:value-of select="LINE_FREE_STOCK"/></freestock_quant>
      </item>
    </position>
  </xsl:if>
</xsl:template>

首先默认项目模板匹配,然后带有 LINE_FREE_STOCK 的项目也匹配带有子 LINE_FREE_STOCK 模板的项目,因此重复的项目带有 LINE_FREE_STOCK。

相反,为什么不只使用一个模板,如下所示:

<xsl:template match="Item">
  <xsl:if test="position() &gt; 1">
    <position>
      <item>
        <number><xsl:value-of select="ITEM_CODE"/></number>
        <xsl:choose>
          <xsl:when test="child::LINE_FREE_STOCK">
            <freestock_quant><xsl:value-of select="LINE_FREE_STOCK"/></freestock_quant>
          </xsl:when>
          <xsl:otherwise>
            <quantity><xsl:value-of select="LINE_QUANTITY"/></quantity>
          </xsl:otherwise>
        </xsl:choose>
      </item>
    </position>
  </xsl:if>
</xsl:template>

使用单一模板,您的订单模板也得到了简化:

<xsl:template match="Order">
  <Header>
    <xsl:value-of select="'some header data'"/>
  </Header>
  <xsl:apply-templates select="Item"/>
</xsl:template>

这样您也不需要使用模式。

于 2011-08-25T14:56:23.727 回答
1

目前尚不清楚想要的输出是什么。也许你想要:

<xsl:apply-templates select="Item[not(LINE_FREE_STOCK)"/>
<xsl:apply-templates select="Item[LINE_FREE_STOCK]" mode="freestock"/>

代替你的

  <xsl:apply-templates select="Item"/>
  <xsl:apply-templates select="Item[child::LINE_FREE_STOCK]" mode="freestock"/>
于 2011-08-25T14:57:32.320 回答
1

你需要一个额外的过滤

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>

<xsl:template match="/">
    <ORDERS05>
        <IDOC BEGIN="1">
            <xsl:apply-templates select="Order"/>
        </IDOC>
    </ORDERS05>
</xsl:template>

<xsl:template match="Order">
    <Header>
        <xsl:value-of select="'some header data'"/>
    </Header>
    <xsl:apply-templates select="Item[not(child::LINE_FREE_STOCK)]"/>
    <xsl:apply-templates select="Item[child::LINE_FREE_STOCK]" mode="freestock"/>
</xsl:template>

<xsl:template match="Item">
    <xsl:if test="position() &gt; 1">
        <position>
            <item>
                <number><xsl:value-of select="ITEM_CODE"/></number>
                <quantity><xsl:value-of select="LINE_QUANTITY"/></quantity>
            </item>
        </position>
    </xsl:if>
</xsl:template>

<xsl:template match="Item[child::LINE_FREE_STOCK]" mode="freestock">
    <xsl:if test="position() &gt; 1">            
        <position>
            <item>
                <number><xsl:value-of select="ITEM_CODE"/></number>
                <freestock_quant><xsl:value-of select="LINE_FREE_STOCK"/></freestock_quant>
            </item>
        </position>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
于 2011-08-25T14:58:11.863 回答