初学者-使用XSLT进行XML到XML的转换

初学者-使用XSLT进行XML到XML的转换,xml,xslt,Xml,Xslt,我有一个视频播放器,由一个php文件提供,该文件从MySql收集数据,并将其输出为xml,如下所示: 当前PHP/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="N

我有一个视频播放器,由一个php文件提供,该文件从MySql收集数据,并将其输出为xml,如下所示:

当前PHP/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>

几周来,我一直在寻找一种使用XSLT转换结构的方法,以便输出是按Gallery,然后按sport进行结构化的。在此方面的任何帮助都将不胜感激

拟议结构

<CONTENT>
  <GALLERY name="John">
    <CATEGORY sport="Soccer">
      <ITEM>
         <vid>1</vid>
      </ITEM>
    </CATEGORY>
    <CATEGORY sport="Golf">
      <ITEM>
        <vid>2</vid>
        <vid>36</vid>
         .....
      </ITEM>
   </CATEGORY>
  <GALLERY/>
  <GALLERY name="sportshound">
    <CATEGORY sport="Golf">
      <ITEM>
        <vid>28</vid>
        <vid>29</vid>
      </ITEM>
    </CATEGORY>
  <GALLERY/>
</CONTENT>

1.
2.
36
.....
28
29

以下是一个示例样式表:

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


要了解有关XSLT 1.0方法(称为Muenchian分组)的更多信息,请访问。

以下是基于新分组功能的XSLT 2.0解决方案

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="CONTENT">
        <CONTENT>
            <xsl:for-each-group select="GALLERY" 
                group-by="@name">
                <GALLERY name="{current-grouping-key()}">
                    <xsl:for-each-group select="current-group()" 
                        group-by="@sport">
                        <CATEGORY sport="{current-grouping-key()}">
                            <ITEM>
                                <xsl:apply-templates select="current-group()"/>
                            </ITEM>
                        </CATEGORY>
                    </xsl:for-each-group>
                </GALLERY>          
            </xsl:for-each-group>
        </CONTENT>
    </xsl:template>

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

</xsl:stylesheet>

生产的产量为:

<CONTENT>
   <GALLERY name="John">
      <CATEGORY sport="Soccer">
         <ITEM>
            <vid>1</vid>
         </ITEM>
      </CATEGORY>
      <CATEGORY sport="Golf">
         <ITEM>
            <vid>2</vid>
            <vid>36</vid>
            <vid>35</vid>
            <vid>34</vid>
            <vid>37</vid>
            <vid>38</vid>
            <vid>39</vid>
            <vid>40</vid>
            <vid>41</vid>
            <vid>42</vid>
            <vid>43</vid>
            <vid>44</vid>
            <vid>45</vid>
            <vid>46</vid>
            <vid>47</vid>
            <vid>48</vid>
            <vid>49</vid>
         </ITEM>
      </CATEGORY>
   </GALLERY>
   <GALLERY name="sportshound">
      <CATEGORY sport="Golf">
         <ITEM>
            <vid>28</vid>
            <vid>29</vid>
         </ITEM>
      </CATEGORY>
   </GALLERY>
</CONTENT>

1.
2.
36
35
34
37
38
39
40
41
42
43
44
45
46
47
48
49
28
29