初学者 - 使用 XSLT 将 XML 转换为 XML [英] Beginner - XML to XML transformation using XSLT

查看:18
本文介绍了初学者 - 使用 XSLT 将 XML 转换为 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a video player which is fed by a php file which gathers data from MySql, and outputs it as xml as below:

当前的 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 结构化.对此的任何帮助将不胜感激!

I have been looking for weeks for a way to transform the structure using XSLT so that the output is structured by Gallery, then by sport. Any help on this would be greatly appreciated!

提议的结构

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

推荐答案

这是一个示例样式表:

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

To read more about that XSLT 1.0 approach called Muenchian grouping visit http://www.jenitennison.com/xslt/grouping/muenchian.xml.

这篇关于初学者 - 使用 XSLT 将 XML 转换为 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆