2015-07-09 194 views
0

我想通過XML元素通過一個公共元素值對數據進行分組。 輸入XML結構爲:使用XSLT 2.0基於元素值和屬性對XML元素進行分組

<?xml version="1.0" encoding="utf-8"?> 
<Data> 
<output outputType="CORRECTION"> 
    <row nodeName="ORDER_HEADER" tableName="ORDER_HEADER"> 
     <column columnName="ORDER_NUMBER">00001</column> 
     <column columnName="Country">England</column> 
     <column columnName="City">London</column> 
    </row> 
    <row nodeName="ORDER_HEADER" tableName="ORDER_HEADER"> 
     <column columnName="ORDER_NUMBER">00002</column> 
     <column columnName="Country">England</column> 
     <column columnName="City">Birmingham</column> 
    </row> 
    <row nodeName="ORDER_DETAIL" tableName="ORDER_DETAIL"> 
     <column columnName="ORDER_NUMBER">00001</column> 
     <column columnName="Book">Gone with the wind</column> 
     <column columnName="Qty">2</column> 
    </row> 
    <row nodeName="ORDER_DETAIL" tableName="ORDER_DETAIL"> 
     <column columnName="ORDER_NUMBER">00001</column> 
     <column columnName="Book">Pride and Prejudice</column> 
     <column columnName="Qty">3</column> 
    </row> 
    <row nodeName="ORDER_DETAIL" tableName="ORDER_DETAIL"> 
     <column columnName="ORDER_NUMBER">00002</column> 
     <column columnName="Book">Jane Eyre</column> 
     <column columnName="Qty">1</column> 
    </row> 
</output> 
</Data> 

我試圖創建以下的輸出:

<Transmission xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://www.w3.org/1999/xhtml"> 
    <OrderNumber>00001</OrderNumber> 
    <Country>England</Country> 
    <City>London</CITY> 
    <Book>Gone with the wind</Book> 
    <QTY>2</QTY> 
    <Book>Pride and Prejudice</Book> 
    <QTY>3</QTY> 
    <OrderNumber>00002</OrderNumber> 
    <Country>England</Country> 
    <City>Birmingham</CITY> 
    <Book>Jane Eyre</Book> 
    <QTY>1</QTY> 
</Transmission> 

這裏是XSL我到目前爲止:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml"> 
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
<xsl:template match="/Data/output[@outputType='CORRECTION']"> 
    <Transmission> 
     <xsl:for-each select="row[@tableName='ORDER_HEADER']"> 
      <OrderNumber> 
       <xsl:value-of select="column[@columnName='ORDER_NUMBER']" /> 
      </OrderNumber> 
      <Country> 
       <xsl:value-of select="column[@columnName='Country']" /> 
      </Country> 
      <City> 
       <xsl:value-of select="column[@columnName='City']" /> 
      </City> 
     </xsl:for-each> 
    </Transmission> 
</xsl:template> 
</xsl:stylesheet> 

我可以輸出來自每個訂單表頭的詳細信息,但無法看到如何選擇和分組正確的訂單詳細信息。任何幫助將非常感激。

感謝

回答

1

您可以使用一鍵查詢訂單詳情

<xsl:key name="detail" match="row[@nodeName='ORDER_DETAIL']" use="column[@columnName='ORDER_NUMBER']" /> 

(在XSLT 1.0和XSLT 2.0其中工程)然後,爲了獲得訂單細節對於給定的順序號,您可以使用鍵像這樣:

<xsl:for-each select="key('detail', column[@columnName='ORDER_NUMBER'])"> 

試試這個XSLT

<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="detail" match="row[@nodeName='ORDER_DETAIL']" use="column[@columnName='ORDER_NUMBER']" /> 
<xsl:template match="/Data/output[@outputType='CORRECTION']"> 
    <Transmission> 
     <xsl:for-each select="row[@tableName='ORDER_HEADER']"> 
      <xsl:variable name="orderNumber" select="column[@columnName='ORDER_NUMBER']" /> 
      <OrderNumber> 
       <xsl:value-of select="$orderNumber" /> 
      </OrderNumber> 
      <Country> 
       <xsl:value-of select="column[@columnName='Country']" /> 
      </Country> 
      <City> 
       <xsl:value-of select="column[@columnName='City']" /> 
      </City> 
      <xsl:apply-templates select="key('detail', $orderNumber)" /> 
     </xsl:for-each> 
    </Transmission> 
</xsl:template> 

<xsl:template match="row"> 
    <Book> 
     <xsl:value-of select="column[@columnName='Book']" /> 
    </Book> 
    <QTY> 
     <xsl:value-of select="column[@columnName='Qty']" /> 
    </QTY> 
</xsl:template> 
</xsl:stylesheet> 

請注意,我改用xsl:apply-templates代替,只是爲了削減太多的嵌套代碼。我還使用了一個變量,以避免重複獲取訂單號。

順便說一句,這真的是你想要的XML輸出嗎?將每個訂單包裝在一個包含父元素的內容中可能更合乎邏輯,否則,爲訂單提取書籍會變得更加困難。

<Transmission> 
<Order> 
    <OrderNumber>00001</OrderNumber> 
    <Country>England</Country> 
    <City>London</City> 
    <Book>Gone with the wind</Book> 
    <QTY>2</QTY> 
    <Book>Pride and Prejudice</Book> 
    <QTY>3</QTY> 
</Order> 
<Order> 
    <OrderNumber>00002</OrderNumber> 
    <Country>England</Country> 
    <City>Birmingham</City> 
    <Book>Jane Eyre</Book> 
    <QTY>1</QTY> 
</Order> 
</Transmission> 
+0

完美!非常感謝您提供清晰簡潔的解決方案。這非常有幫助。 –