2010-08-11 181 views
1

我的XML看起來像使用XSLT 1.0分組

<xml version="1.0" standalone="no"> 
<Alerts AlertsName=""> 
<Alert UserId="13" OwnerId="13" OwnerName="jasprice" OwnerEmail="[email protected]" Scope="I"> 
<Assets> 
<Asset> 
<AssetTaxonamy> 
<AssetTopics> 
    <AssetTopic Type="Global business">Globalisation</AssetTopic> 
    </AssetTopics> 
    </AssetTaxonamy> 
    <AssetTitle>Doing it the Chinese way: Disney's strategy for a lucrative ride in China</AssetTitle> 
    <AssetDescription>FROM INSEAD KNOWLEDGE As trite as the phrase ?think global, act local? may be, it nevertheless encapsulates The Walt Disney Company?s approach to making a success of its business in China. To that end, producing content that has a cultural and emotional resonance with Chinese consumers is crucial, says Stanley Cheung, Disney?s managing director for Greater China. ?We see it in China today, that ...</AssetDescription> 
    <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=467282231</AssetLink> 
    <AssetPubDate>7/23/2010</AssetPubDate> 
    <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
    <AssetSource>Executive briefings/whitepapers</AssetSource> 
    </Asset> 
<Asset> 
<AssetTaxonamy> 
<AssetTopics> 
    <AssetTopic Type="Sales and marketing">Marketing effectiveness</AssetTopic> 
    </AssetTopics> 
    </AssetTaxonamy> 
    <AssetTitle>The empowered sales team: Enhancing productivity through thebetter use of information</AssetTitle> 
    <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Sales professionals create value by transforming their company?s raw product or service into a tailored solution for a customer?s needs. In a setting where customers have access to an unprecedented amount of product information, this is an increasingly challenging task. To thrive in this environment, companies...</AssetDescription> 
    <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1297294514</AssetLink> 
    <AssetPubDate>7/23/2010</AssetPubDate> 
    <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
    <AssetSource>Executive briefings/whitepapers</AssetSource> 
    </Asset> 
<Asset> 
<AssetTaxonamy> 
<AssetTopics> 
    <AssetTopic Type="Sales and marketing">Customer management</AssetTopic> 
    </AssetTopics> 
    </AssetTaxonamy> 
    <AssetTitle>On the front lines: The role of information in enhancing customer service</AssetTitle> 
    <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Customer service professionals play a critical role in most companies because they are the people customers are most often in contact with, especially when problems arise. In today?s unsettled market environment, the ability to build a deep understanding of evolving customer needs and expectations can make a com...</AssetDescription> 
    <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1327294517</AssetLink> 
    <AssetPubDate>7/23/2010</AssetPubDate> 
    <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
    <AssetSource>Executive briefings/whitepapers</AssetSource> 
    </Asset> 
<Asset> 
<AssetTaxonamy> 
<AssetTopics> 
    <AssetTopic Type="Leadership">Managing change</AssetTopic> 
    </AssetTopics> 
    </AssetTaxonamy> 
    <AssetTitle>World of leaders</AssetTitle> 
    <AssetDescription>FROM INDIAN SCHOOL OF BUSINESS At the inaugural ?ISB World of Leaders? speaker series event hosted by the Indian School of Business (ISB), Henry R Kravis, Co-Founder, Co-Chairman and Co-CEO, Kohlberg Kravis Roberts &amp; Company (KKR), New York, interacted with a select group of Indian business leaders about how he started his enterprise, the journey so far and his views on the private equity industr...</AssetDescription> 
    <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1757259560</AssetLink> 
    <AssetPubDate>7/23/2010</AssetPubDate> 
    <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
    <AssetSource>Executive briefings/whitepapers</AssetSource> 
    </Asset> 
<Asset> 
<AssetTaxonamy> 
<AssetTopics> 
    <AssetTopic Type="Global business">Country strategies</AssetTopic> 
    </AssetTopics> 
    </AssetTaxonamy> 
    <AssetTitle>State industries in Lithuania: Follow the money</AssetTitle> 
    <AssetDescription>State-owned enterprises in eastern Europe are often a mess. Lithuania is trying to reform them DIG into the political undergrowth anywhere between the Baltic and the Black Seas and you soon find curious connections between state-owned enterprises, officials and politicians. They mostly escape public scrutiny--not least because politicians of all stripes tend to benefit from the state firms? large...</AssetDescription> 
    <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=67277991</AssetLink> 
    <AssetPubDate>7/23/2010</AssetPubDate> 
    <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
    <AssetSource>Executive briefings/whitepapers</AssetSource> 
    </Asset> 
</Assets> 
</Alert> 
</Alerts> 

現在,我們需要通過第一種類型的屬性(如全球化)集團的資產,然後按值(前國家stratagies)需要組。我們只需要顯示一次全球化和國家戰略,並且需要顯示下面的所有資產。我可以輕鬆地用XSLT 2.0來完成它,但它不支持Microsoft .net,因此無法使用。

任何人都可以幫助我嗎?

在此先感謝。

+0

粘貼代碼時請多加小心 - 至少格式化它的可讀性,而不是直接從IE瀏覽器進行格式化。 – Oded 2010-08-11 12:44:50

+0

謝謝。改變。 – jvm 2010-08-11 13:10:17

+0

好問題(+1)。查看我的答案以獲得完整高效的XSLT 1.0解決方案。 – 2010-08-11 13:43:08

回答

1

該轉化

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="kAssetByTypeName" match="Asset" 
    use="AssetTaxonamy/AssetTopics/AssetTopic[1]/@Type"/> 

<xsl:key name="kAssetByTypeNameValue" match="Asset" 
    use="concat(AssetTaxonamy/AssetTopics/AssetTopic[1]/@Type, 
       AssetTaxonamy/AssetTopics/AssetTopic[1] 
      ) 
    "/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Assets"> 
    <Assets> 
    <xsl:for-each select= 
    "Asset[generate-id() 
      = 
      generate-id(key('kAssetByTypeName', 
          AssetTaxonamy/AssetTopics 
             /AssetTopic[1]/@Type 
          ) 
          [1] 
         ) 
      ] 
    "> 
     <xsl:variable name="vcurType" select= 
     "AssetTaxonamy/AssetTopics/AssetTopic[1]/@Type"/> 

     <Topic Type="{$vcurType}"> 
      <xsl:for-each select= 
      "key('kAssetByTypeName',$vcurType) 
       [generate-id() 
       = 
       generate-id(key('kAssetByTypeNameValue', 
            concat($vcurType, 
             AssetTaxonamy/AssetTopics/AssetTopic[1] 
             ) 
           )[1] 
          ) 
       ] 
      "> 
      <xsl:variable name="vSubject" select= 
       "AssetTaxonamy/AssetTopics/AssetTopic[1]"/> 

      <Subject> 
       <xsl:value-of select="$vSubject"/> 
      </Subject> 

      <xsl:apply-templates select= 
       "key('kAssetByTypeNameValue', 
        concat($vcurType, $vSubject) 
        ) 
       "/> 
      </xsl:for-each> 
     </Topic> 
    </xsl:for-each> 
    </Assets> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的(經校正的良好成形以)的XML文檔施加:

<Alerts AlertsName=""> 
    <Alert UserId="13" OwnerId="13" OwnerName="jasprice" OwnerEmail="[email protected]" Scope="I"> 
     <Assets> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
         <AssetTopic Type="Global business">Globalisation</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>Doing it the Chinese way: Disney's strategy for a lucrative ride in China</AssetTitle> 
       <AssetDescription>FROM INSEAD KNOWLEDGE As trite as the phrase ?think global, act local? may be, it nevertheless encapsulates The Walt Disney Company?s approach to making a success of its business in China. To that end, producing content that has a cultural and emotional resonance with Chinese consumers is crucial, says Stanley Cheung, Disney?s managing director for Greater China. ?We see it in China today, that ...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=467282231</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
         <AssetTopic Type="Sales and marketing">Marketing effectiveness</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>The empowered sales team: Enhancing productivity through thebetter use of information</AssetTitle> 
       <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Sales professionals create value by transforming their company?s raw product or service into a tailored solution for a customer?s needs. In a setting where customers have access to an unprecedented amount of product information, this is an increasingly challenging task. To thrive in this environment, companies...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1297294514</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
         <AssetTopic Type="Sales and marketing">Customer management</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>On the front lines: The role of information in enhancing customer service</AssetTitle> 
       <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Customer service professionals play a critical role in most companies because they are the people customers are most often in contact with, especially when problems arise. In today?s unsettled market environment, the ability to build a deep understanding of evolving customer needs and expectations can make a com...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1327294517</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
         <AssetTopic Type="Leadership">Managing change</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>World of leaders</AssetTitle> 
       <AssetDescription>FROM INDIAN SCHOOL OF BUSINESS At the inaugural ?ISB World of Leaders? speaker series event hosted by the Indian School of Business (ISB), Henry R Kravis, Co-Founder, Co-Chairman and Co-CEO, Kohlberg Kravis Roberts &amp; Company (KKR), New York, interacted with a select group of Indian business leaders about how he started his enterprise, the journey so far and his views on the private equity industr...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1757259560</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
         <AssetTopic Type="Global business">Country strategies</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>State industries in Lithuania: Follow the money</AssetTitle> 
       <AssetDescription>State-owned enterprises in eastern Europe are often a mess. Lithuania is trying to reform them DIG into the political undergrowth anywhere between the Baltic and the Black Seas and you soon find curious connections between state-owned enterprises, officials and politicians. They mostly escape public scrutiny--not least because politicians of all stripes tend to benefit from the state firms? large...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=67277991</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
     </Assets> 
    </Alert> 
</Alerts> 

產生想要分組

<Alerts AlertsName=""> 
    <Alert UserId="13" OwnerId="13" OwnerName="jasprice" OwnerEmail="[email protected]" Scope="I"> 
     <Assets> 
     <Topic Type="Global business"> 
      <Subject>Globalisation</Subject> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
        <AssetTopic Type="Global business">Globalisation</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>Doing it the Chinese way: Disney's strategy for a lucrative ride in China</AssetTitle> 
       <AssetDescription>FROM INSEAD KNOWLEDGE As trite as the phrase ?think global, act local? may be, it nevertheless encapsulates The Walt Disney Company?s approach to making a success of its business in China. To that end, producing content that has a cultural and emotional resonance with Chinese consumers is crucial, says Stanley Cheung, Disney?s managing director for Greater China. ?We see it in China today, that ...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=467282231</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Subject>Country strategies</Subject> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
        <AssetTopic Type="Global business">Country strategies</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>State industries in Lithuania: Follow the money</AssetTitle> 
       <AssetDescription>State-owned enterprises in eastern Europe are often a mess. Lithuania is trying to reform them DIG into the political undergrowth anywhere between the Baltic and the Black Seas and you soon find curious connections between state-owned enterprises, officials and politicians. They mostly escape public scrutiny--not least because politicians of all stripes tend to benefit from the state firms? large...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=67277991</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
     </Topic> 
     <Topic Type="Sales and marketing"> 
      <Subject>Marketing effectiveness</Subject> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
        <AssetTopic Type="Sales and marketing">Marketing effectiveness</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>The empowered sales team: Enhancing productivity through thebetter use of information</AssetTitle> 
       <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Sales professionals create value by transforming their company?s raw product or service into a tailored solution for a customer?s needs. In a setting where customers have access to an unprecedented amount of product information, this is an increasingly challenging task. To thrive in this environment, companies...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1297294514</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
      <Subject>Customer management</Subject> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
        <AssetTopic Type="Sales and marketing">Customer management</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>On the front lines: The role of information in enhancing customer service</AssetTitle> 
       <AssetDescription>FROM THE ECONOMIST INTELLIGENCE UNIT Sponsored by Microsoft Introduction Customer service professionals play a critical role in most companies because they are the people customers are most often in contact with, especially when problems arise. In today?s unsettled market environment, the ability to build a deep understanding of evolving customer needs and expectations can make a com...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1327294517</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
     </Topic> 
     <Topic Type="Leadership"> 
      <Subject>Managing change</Subject> 
      <Asset> 
       <AssetTaxonamy> 
        <AssetTopics> 
        <AssetTopic Type="Leadership">Managing change</AssetTopic> 
        </AssetTopics> 
       </AssetTaxonamy> 
       <AssetTitle>World of leaders</AssetTitle> 
       <AssetDescription>FROM INDIAN SCHOOL OF BUSINESS At the inaugural ?ISB World of Leaders? speaker series event hosted by the Indian School of Business (ISB), Henry R Kravis, Co-Founder, Co-Chairman and Co-CEO, Kohlberg Kravis Roberts &amp; Company (KKR), New York, interacted with a select group of Indian business leaders about how he started his enterprise, the journey so far and his views on the private equity industr...</AssetDescription> 
       <AssetLink>http://viewswire.eiu.com/index.asp?layout=EBArticleVW3&amp;article_id=1757259560</AssetLink> 
       <AssetPubDate>7/23/2010</AssetPubDate> 
       <AssetArrivalDate>8/9/2010</AssetArrivalDate> 
       <AssetSource>Executive briefings/whitepapers</AssetSource> 
      </Asset> 
     </Topic> 
     </Assets> 
    </Alert> 
</Alerts> 

請注意:使用Muenchian方法進行分組。

+0

偉大的答案Dimitre。但這裏的問題是,我有XML和我將只做XSLT轉換,它應該生成所需的HTML。你的答案是完美的,但它會增加一個步驟,我不能適應我的業務解決方案。你能再提一個嗎? – jvm 2010-08-11 13:59:47

+0

我們需要HTML輸出像

全球商業 國家戰略 AssetTitle1 AssetDescp1 AssetTitle2 AssetDescp2全球化 AssetTitle1 AssetDescp1 AssetTitle2 AssetDescp2
其他羣體同樣的方式........... – jvm 2010-08-11 14:16:00

+0

@賈丁:我*已*解決你的問題roblem - 嵌套分組。這個組不是關於HTML的。如果您不知道要使用什麼HTML,只需向HTML組中的人員詢問即可。您的* XSLT *問題已完全解答。更何況,你從來沒有提到你的原始問題,你需要HTML輸出! – 2010-08-11 14:28:43