1

我一直在查看 XSLT 1.0 中的 Muenchian 分组示例,特别是这里的示例。但是我无法让它在更复杂的 XML 结构上工作。

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

但是,当它们具有相同的 Sport 和 Event 时,我想将 Rank 节点分组到相同的 Ranks 父级下。所以我希望结果看起来像这样:

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

我只是有点迷失如何做到这一点,因为唯一的其他例子是处理一个更简单的结构,我不确定它是否可能或者我的密钥和模板需要如何构建才能做到这一点。谁能提供一些示例如何实现?

任何意见,将不胜感激。

4

1 回答 1

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 分组方法并覆盖身份规则

于 2012-08-02T13:30:41.207 回答