如何从基于属性的平面XML中进行(两级)Meuncian分组
上周我在这个论坛上发布了一个问题,得到了非常好的回复,但我意识到我没有深入到足够的深度,所以,在又一周毫无结果的自我搜索之后,我希望我能在这里找到一些帮助 我现在有了一个XSL样式表,它可以将PHP生成的XML转换成一种可读的格式,作为视频播放器的播放列表。本质上,这将平面结构转换为具有两层层次结构的结构 我之前忘记提到的是,我需要第三层的层次结构user->sport->videoID,这样我就可以挂起每个视频的视频属性 原始XML 拟议结构 当前XSL样式表如何从基于属性的平面XML中进行(两级)Meuncian分组,xml,xslt,Xml,Xslt,上周我在这个论坛上发布了一个问题,得到了非常好的回复,但我意识到我没有深入到足够的深度,所以,在又一周毫无结果的自我搜索之后,我希望我能在这里找到一些帮助 我现在有了一个XSL样式表,它可以将PHP生成的XML转换成一种可读的格式,作为视频播放器的播放列表。本质上,这将平面结构转换为具有两层层次结构的结构 我之前忘记提到的是,我需要第三层的层次结构user->sport->videoID,这样我就可以挂起每个视频的视频属性 原始XML 拟议结构 当前XSL样式表 实际上,您不需要第三个分组,只需
实际上,您不需要第三个分组,只需以不同的方式包装第三级项目 在变换中,您需要替换:
<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>
与:
您建议的输出是无效的XML:上一个问题中的答案也回答了这个问题。@Jim的评论+1,尽管从技术上讲,他指的是格式不正确的XML,而不是无效的XML。
<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: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>
<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:template match="GALLERY" mode="sport">
<CATEGORY sport="{@sport}">
<xsl:apply-templates select="
key('k2', concat(@name, '|', @sport))"
mode="item"/>
</CATEGORY>
</xsl:template>
<xsl:template match="GALLERY" mode="item">
<ITEM>
<vid>
<xsl:value-of select="@vid"/>
</vid>
<vidtitle>
<xsl:value-of select="@vidtitle"/>
</vidtitle>
</ITEM>
</xsl:template>