【问题标题】:Beginner - XML to XML transformation using XSLT初学者 - 使用 XSLT 进行 XML 到 XML 的转换
【发布时间】:2011-06-21 12:13:09
【问题描述】:

我有一个视频播放器,它由一个 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>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    这是一个示例样式表:

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

    要了解有关称为 Muenchian 分组的 XSLT 1.0 方法的更多信息,请访问 http://www.jenitennison.com/xslt/grouping/muenchian.xml

    【讨论】:

      【解决方案2】:

      这是基于新分组功能的 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-02
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多