如何从基于属性的平面XML(两级)Meunchian分组

时间:2011-06-28 14:24:52

标签: xml xslt

我上周在这个论坛上发了一个问题,得到了非常好的回复,但我意识到我没有深入探讨,所以,经过又一周徒劳无功的搜索,我希望我能在这里找到一些帮助

Beginner - XML to XML transformation using XSLT

我现在有一个XSL样式表,它将PHP生成的XML转换为可读作为视频播放器播放列表的表单。从本质上讲,这会将平面结构转换为具有两层层次结构的平面结构(用户 - >运动)。

我之前忘记提到的是我需要第三级层次结构(用户 - > sport - > videoID),以便我可以将视频属性挂起来。

原始XML

   <?xml version="1.0" ?>  
<CONTENT>   
<GALLERY name="John" vid="1" vidtitle="NL - 22nd Jan 2011 - FO sport="Soccer" />       
<GALLERY name="John" vid="2" vidtitle="NL - 22nd Jan 2011 - DL" sport="Golf" />
<GALLERY name="sportshound" vid="28" vidtitle="Tiger Woods" sport="Golf" />    
<GALLERY name="sportshound" vid="29" vidtitle="Tigerwoodstest" sport="Golf" />    
<GALLERY name="John" vid="36" vidtitle="5 iron behind April" sport="Golf" />    
<GALLERY name="John" vid="35" vidtitle="face on april" sport="Golf" />    
<GALLERY name="John" vid="34" vidtitle="wqetfgtgdijuserf" sport="Golf" />    
<GALLERY name="John" vid="37" vidtitle="April - 3 iron Behind" sport="Golf" />    
<GALLERY name="John" vid="38" vidtitle="April - 7 iron behind" sport="Golf" />    
<GALLERY name="John" vid="39" vidtitle="April - 3 wood behind" sport="Golf" />    
<GALLERY name="John" vid="40" vidtitle="24 April - 7 iron behind" sport="Golf" />    <GALLERY name="John" vid="41" vidtitle="April 29 Iron behind swing left" sport="Golf" />    
<GALLERY name="John" vid="42" vidtitle="29 April iron behind shallowing" sport="Golf" />    
<GALLERY name="John" vid="43" vidtitle="1st May Driver Behind" sport="Golf" />    
<GALLERY name="John" vid="44" vidtitle="21st May - 6I behind - swing left" sport="Golf" />    
<GALLERY name="John" vid="45" vidtitle="Adam Scott - Masters '11 - iron behind" sport="Golf" />    
<GALLERY name="John" vid="46" vidtitle="19th June 2011 - Face on - impact" sport="Golf" />    
<GALLERY name="John" vid="47" vidtitle="19 June - Behind - 6i" sport="Golf" />    
<GALLERY name="John" vid="48" vidtitle="19 June 2011 - Face on - 8i (impact)" sport="Golf" />    
<GALLERY name="John" vid="49" vidtitle="19 June 2011 - Face On - 5i (impact)" sport="Golf" />    
</CONTENT> 

建议的结构

<CONTENT>   
  <GALLERY name="John">     
   <CATEGORY sport="Soccer">       
     <ITEM>          
      <vid>1</vid>   
      <vidtitle>NL - 22nd Jan 2011 - FO</vidtitle>
     </ITEM>     
   </CATEGORY>     
   <CATEGORY sport="Golf">       
     <ITEM>         
       <vid>2</vid>         
       <vidtitle>NL - 22nd Jan 2011 - DL</vidtitle>
     </ITEM>
     <ITEM>
       <vid>36</vid>   
       <vidtitle>NL - 22nd Jan 2011 - DL</vidtitle>
     </ITEM>
    ............      
   </CATEGORY>   
  <GALLERY/>   
  <GALLERY name="sportshound">     
   <CATEGORY sport="Golf">       
    <ITEM>         
      <vid>28</vid>         
      <vidtitle>Tigerwoodstest</vid>       
    </ITEM>     
     .........
   </CATEGORY>   
  <GALLERY/> 
</CONTENT> 

当前的XSL样式表

    <xsl:stylesheet   
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
version="1.0">    
<xsl:output indent="yes"/>    
<xsl:key name="k1" match="GALLERY" use="@name"/>   
<xsl:key name="k2" match="GALLERY" use="concat(@name, '|', @sport)"/>    
<xsl:template match="CONTENT">     
<xsl:copy>       
<xsl:apply-templates select="GALLERY[generate-id() = generate-id(key('k1', @name)[1])]"/>     
</xsl:copy>   
</xsl:template>    
<xsl:template match="GALLERY">     
<GALLERY name="{@name}">       
<xsl:apply-templates select="key('k1', @name)[generate-id() = generate-id(key('k2', concat(@name, '|', @sport))[1])]" mode="sport"/>     
</GALLERY>   
</xsl:template>    
<xsl:template match="GALLERY" mode="sport">     
<CATEGORY sport="{@sport}">       
<ITEM>         
<xsl:apply-templates select="key('k2', concat(@name, '|', @sport))/@vid"/>       </ITEM>     
</CATEGORY>   
</xsl:template>    
<xsl:template match="GALLERY/@vid">     
<vid>       
<xsl:value-of select="."/>     
</vid>   
</xsl:template>  
</xsl:stylesheet> 

1 个答案:

答案 0 :(得分:0)

实际上你不需要第三次分组,只是以不同方式包装第三级项目。

在转换中,您需要替换:

<xsl:template match="GALLERY" mode="sport">     
    <CATEGORY sport="{@sport}">       
        <ITEM>         
            <xsl:apply-templates select="
              key('k2', concat(@name, '|', @sport))/@vid"/>      
        </ITEM>     
    </CATEGORY>   
</xsl:template>    

<xsl:template match="GALLERY/@vid">     
    <vid>       
        <xsl:value-of select="."/>     
    </vid>   
</xsl:template>  

使用:

    <xsl:template match="GALLERY" mode="sport">     
        <CATEGORY sport="{@sport}">          
            <xsl:apply-templates select="
              key('k2', concat(@name, '|', @sport))" 
              mode="item"/> 
        </CATEGORY>   
    </xsl:template>    

    <xsl:template match="GALLERY" mode="item"> 
        <ITEM>
            <vid>       
                <xsl:value-of select="@vid"/>     
            </vid>
            <vidtitle>
                <xsl:value-of select="@vidtitle"/>
            </vidtitle>
        </ITEM>     
    </xsl:template>