2017-04-26 60 views
0

我需要一些編寫xsl的幫助。如何編寫xsl以獲取所有重複標籤的值?

我有如下的XML數據。

<Data> 
    <so_offer_display_value action="add">XXXX</so_offer_display_value> 
    <so_offer_id action="add">51005577</so_offer_id> 
    <so_offer_view_id action="add">3932079043</so_offer_view_id> 
    <so_offer_display_value action="add">YYYYY</so_offer_display_value> 
    <so_offer_id action="add">51005541</so_offer_id> 
    <so_offer_view_id action="add">3932080043</so_offer_view_id> 
</Data> 

我想要的輸出是:

<inst> 
    <offerId>51005577</offerId> 
    <instId>3932079043</instId> 
    <Description>XXXX</Description> 
    <Action>Add</Action> 
</inst> 
<inst> 
    <offerId>51005541</offerId> 
    <instId>3932080043</instId> 
    <Description>YYYY</Description> 
    <Action>Add</Action> 
</inst> 

我試圖像下面,但它打印相同值的兩倍。

<xsl:if test="/Data/so_offer_id/@action = 'add'"> 
    <xsl:for-each select="/Data/so_offer_id"> 
    <inst> 
     <offerId> 
     <xsl:value-of select="/Data/so_offer_id[@action='add']"/> 
     </offerId> 
     <instId> 
     <xsl:value-of select="/Data/so_offer_view_id[@action='add']"/> 
     </instId> 
     <Description> 
     <xsl:value-of select="/Data/so_offer_display_value[@action='add']"/> 
     </Description> 
     <Action>Add</Action> 
     <Quantity>1</Quantity> 
    </inst> 
    </xsl:for-each> 
</xsl:if> 

回答

1

下面您有一個解決方案基於密鑰。 該解決方案的優點是Data中的源標籤可以以任意順序排列。

第一個輸出inst標籤包含具有各自名稱的第一個標籤。 第二個輸出inst標籤包含第二個標籤等。

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

    <xsl:key name="tags" match="Data/*" use="name()"/> 

    <xsl:template match="Data"> 
    <xsl:copy> 
     <xsl:for-each select="so_offer_id"> 
     <xsl:element name="inst"> 
      <xsl:variable name="pos" select="position()"/> 
      <xsl:element name="offerId"> 
      <xsl:value-of select="."/> 
      </xsl:element> 
      <xsl:element name="instId"> 
      <xsl:value-of select="key('tags', 'so_offer_view_id')[$pos]"/> 
      </xsl:element> 
      <xsl:element name="Description"> 
      <xsl:value-of select="key('tags', 'so_offer_display_value')[$pos]"/> 
      </xsl:element> 
      <Action>Add</Action> 
     </xsl:element> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

鑰匙在這裏並沒有真正做到有用的東西。而不是''你可以簡單地說''。 –

+0

P.S.我不認爲你可以單獨使用索引,因爲問題表明可能存在其「行爲」不是「添加」的干預項目。但是,如果OP是幸福的... –

+0

我認爲*鍵*比普通的模式選擇更快。這對於大量輸入尤其重要。 –

0

我猜你想做的事:

XSLT 1.0

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

<xsl:template match="/Data"> 
    <xsl:for-each select="so_offer_id[@action='add']"> 
     <inst> 
      <offerId> 
       <xsl:value-of select="."/> 
      </offerId> 
      <instId> 
       <xsl:value-of select="following-sibling::so_offer_view_id[1]"/> 
      </instId> 
      <Description> 
       <xsl:value-of select="preceding-sibling::so_offer_display_value[1]"/> 
      </Description> 
      <Action>Add</Action> 
      <Quantity>1</Quantity> 
     </inst> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

這是假設提供的每個實例具有相同的3個節點,以相同的順序。

請注意,結果不是格式良好的XML文檔,因爲它沒有單個根元素。

+0

有沒有什麼辦法可以進一步改進以擺脫輸入xml的排序?在我的情況下,輸入xml將由另一個作業生成,並且可以按任何順序顯示。 – vivek

+0

然後,您需要澄清如何識別需要一起分組到相同'inst'的節點。 –

相關問題