我有一个视频播放器,它由一个从MySql收集数据的php文件提供,并将其输出为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构成,然后是运动。任何有关这方面的帮助将不胜感激!
建议的结构
<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>
答案 0 :(得分:3)
以下是样式表示例:
<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。
答案 1 :(得分:1)
以下是基于新分组功能的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>