0
上週我在這個論壇上發佈了一個問題,得到了非常好的迴應,但是我意識到我沒有深入到足夠的深度,所以,之後又一週沒有成果,我希望我能在這裏找到一些幫助。如何從基於屬性的平面XML(雙層)Meunchian分組
Beginner - XML to XML transformation using XSLT
我現在有一個XSL樣式表,其將PHP生成的XML成形式,其可讀作爲用於視頻播放器的播放列表。實質上,這將平面結構轉換爲兩層結構(用戶 - >運動)。
我之前忘記提到的是我需要第三層次的層次結構(用戶 - >運動 - > 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>
你的 「建議輸出」 是不是有效的XML:''請編輯您的文章並更正它。我懷疑你真的想' NL - 2011年1月22日 - FO ' –
上一個問題的答案也回答了這個問題。 –
+1 to @ Jim的評論,儘管在技術上他的意思是「沒有格式良好的XML」而不是「無效的XML」。 – LarsH