2012-08-02 159 views
1

我一直在尋找XSLT 1.0中Muenchian分組的示例,特別是此示例here。但是我無法使它在更復雜的XML結構上工作。在xslt 1.0中分組嵌套元素

我的XML目前看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<ContestResults> 
    <Contests> 
    <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
     <Rank position="1" eventId="1"> 
      <Athlete>Athlete 1a/Athlete 2a [GER]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="1"> 
      <Athlete>Athlete 1b/Athlete 2b [NED]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    <Contest sportId="32"> 
     <Sport>Tennis</Sport> 
     <Event>Women's Singles</Event> 
     <Ranks> 
     <Rank position="1" eventId="2"> 
      <Athlete>Tennis Athlete 1</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="2"> 
      <Athlete>Tennis Athlete 2</Athlete> 
      <Result>1</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
     <Rank position="1" eventId="3"> 
      <Athlete>Athlete 3a/Athlete 4a [AUT]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="3"> 
      <Athlete>Athlete 3b/Athlete 4b [SUI]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    </Contests> 
</ContestResults> 

但是我想組相同的隊伍母公司下的排名節點時,它們具有相同的體育和活動。所以,我想要的結果看起來像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<ContestResults> 
    <Contests> 
    <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
     <Rank position="1" eventId="1"> 
      <Athlete>Athlete 1a/Athlete 2a [GER]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="1"> 
      <Athlete>Athlete 1b/Athlete 2b [NED]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     <Rank position="1" eventId="3"> 
      <Athlete>Athlete 3a/Athlete 4a [AUT]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="3"> 
      <Athlete>Athlete 3b/Athlete 4b [SUI]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    <Contest sportId="32"> 
     <Sport>Tennis</Sport> 
     <Event>Women's Singles</Event> 
     <Ranks> 
     <Rank position="1" eventId="2"> 
      <Athlete>Tennis Athlete 1</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="2"> 
      <Athlete>Tennis Athlete 2</Athlete> 
      <Result>1</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    </Contests> 
</ContestResults> 

我只是有點失去了如何做到這一點的唯一其他例子是在處理一個更簡單的結構,我不知道是否有可能或我的鑰匙和模板如何構建才能做到這一點。任何人都可以提供一些例子來說明如何實現?

任何意見,將不勝感激。

回答

0

這種轉變

<xsl:stylesheet version="1.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="kContestById" match="Contest" use="@sportId"/> 

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

<xsl:template match="Contests"> 
     <Contests> 
     <xsl:apply-templates/> 
     </Contests> 
</xsl:template> 

<xsl:template match= 
    "Contest 
    [not(generate-id() 
    = 
    generate-id(key('kContestById', @sportId)[1])) 
    ]"/> 
<xsl:template match="Ranks"> 
    <Ranks> 
    <xsl:apply-templates select="key('kContestById', ../@sportId)/Ranks/Rank"/> 
    </Ranks> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<ContestResults> 
    <Contests> 
    <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
     <Rank position="1" eventId="1"> 
      <Athlete>Athlete 1a/Athlete 2a [GER]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="1"> 
      <Athlete>Athlete 1b/Athlete 2b [NED]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    <Contest sportId="32"> 
     <Sport>Tennis</Sport> 
     <Event>Women's Singles</Event> 
     <Ranks> 
     <Rank position="1" eventId="2"> 
      <Athlete>Tennis Athlete 1</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="2"> 
      <Athlete>Tennis Athlete 2</Athlete> 
      <Result>1</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
     <Rank position="1" eventId="3"> 
      <Athlete>Athlete 3a/Athlete 4a [AUT]</Athlete> 
      <Result>2</Result> 
     </Rank> 
     <Rank position="2" eventId="3"> 
      <Athlete>Athlete 3b/Athlete 4b [SUI]</Athlete> 
      <Result>0</Result> 
     </Rank> 
     </Ranks> 
    </Contest> 
    </Contests> 
</ContestResults> 

產生想要的,正確的結果:

<ContestResults> 
    <Contests> 
     <Contest sportId="35"> 
     <Sport>Beach Volleyball</Sport> 
     <Event>Men's</Event> 
     <Ranks> 
      <Rank position="1" eventId="1"> 
       <Athlete>Athlete 1a/Athlete 2a [GER]</Athlete> 
       <Result>2</Result> 
      </Rank> 
      <Rank position="2" eventId="1"> 
       <Athlete>Athlete 1b/Athlete 2b [NED]</Athlete> 
       <Result>0</Result> 
      </Rank> 
      <Rank position="1" eventId="3"> 
       <Athlete>Athlete 3a/Athlete 4a [AUT]</Athlete> 
       <Result>2</Result> 
      </Rank> 
      <Rank position="2" eventId="3"> 
       <Athlete>Athlete 3b/Athlete 4b [SUI]</Athlete> 
       <Result>0</Result> 
      </Rank> 
     </Ranks> 
     </Contest> 
     <Contest sportId="32"> 
     <Sport>Tennis</Sport> 
     <Event>Women's Singles</Event> 
     <Ranks> 
      <Rank position="1" eventId="2"> 
       <Athlete>Tennis Athlete 1</Athlete> 
       <Result>2</Result> 
      </Rank> 
      <Rank position="2" eventId="2"> 
       <Athlete>Tennis Athlete 2</Athlete> 
       <Result>1</Result> 
      </Rank> 
     </Ranks> 
     </Contest> 
    </Contests> 
</ContestResults> 

說明

正確使用的Muenchian grouping method並重寫identity rule

+0

感謝您的幫助。這很好,但我需要更新我的鑰匙才能使用Sport AND Event,以確保我沒有將錯誤的事件組合在一起,即concat(Sport,'+',Event) – pat007 2012-08-03 00:36:09

+0

@ pat007:不客氣。至於不使用組合鍵進行分組,所提供的XML文檔和想要的結果沒有提示這將是必需的 - 如果它是這樣的話 - 當然,這很容易實現。 – 2012-08-03 01:41:06