XSLT 1.0: apply-templates 和 template mode

3

I have the following 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标签中,我会有<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

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

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

如果有一个带有子元素LINE_FREE_STOCK的子元素Item,则该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>

你好Dimitre,谢谢你的回答。抱歉,我没有表达清楚我的问题。我得到的输出正是我想要的。每个LINE_QUANTITY和LINE_FREE_STOCK都有一个位置。我只是想确认一下我做得对不对。不过,我已经改变了我的代码,使用"Item[position() >1]"来摆脱xsl:if。你的帖子总是详细而有趣,我很喜欢阅读它们!谢谢你,祝你一切顺利。Peter - Peter

1

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

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

首先,使用默认的Item模板匹配,然后使用带有LINE_FREE_STOCK的Item也匹配具有子LINE_FREE_STOCK模板的Item,因此会出现带有LINE_FREE_STOCK的Item的重复。

为什么不使用一个模板呢?像这样:

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

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


你好,保罗。感谢你的回复。抱歉,我没有表达清楚我的问题。我只想要修改我的代码。我得到的输出是我想要的。每个“LINE_QUANTITY”和每个“LINE_FREE_STOCK”都有一个记录集。对于你的努力给予1+ - 谢谢,彼得。 - Peter

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"/>

1
你好 empo,是的 - 你说得对。我没有表达清楚我的问题。对此我感到抱歉。我得到的输出是我想要的。我只是想确认一下这是正确的方法。为你的努力加1分,祝一切顺利,Peter - Peter

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>

你好user905927,感谢你的回复。很抱歉,我的问题表述得不够清楚。我只想让我的代码进行修订。我得到的输出就是我想要的。每个“LINE_QUANTITY”和“LINE_FREE_STOCK”都有一个记录集。对于你的努力给予1+赞 - 谢谢,Peter - Peter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接