我有一个XML文件,我需要排序。工作得很好,直到开发人员告诉我将XML更改为具有type = label属性的项目以标记节点。在XSLT方面不太好。需要在“排序”节点上排序。XSLT xsl:排序问题
(简化)XML看起来是这样的:
<rss>
<channel>
<title>This is the title</title>
<link>http://www.mydomain.com/</link>
<description>The Description</description>
<label>
<title>Another Label</title>
<sort>4</sort>
</label>
<item>
<title>An Item</title>
<sort>2</sort>
</item>
<item>
<title>One Item</title>
<sort>3</sort>
</item>
<label>
<title>A Label</title>
<sort>1</sort>
</label>
</channel>
</rss>
旧的XSL(当我刚整理 '项目')看起来是这样的:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="channel">
<rss>
<channel>
<xsl:copy-of select="title"/>
<xsl:copy-of select="link"/>
<xsl:copy-of select="description"/>
<xsl:apply-templates select="item">
<xsl:sort select="sort" data-type="number"/>
</xsl:apply-templates>
</channel>
</rss>
</xsl:template>
<xsl:template match="item">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
试过这种思维会工作,它大部分是,但我得到各种“落后者”。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="channel">
<rss>
<channel>
<xsl:copy-of select="title"/>
<xsl:copy-of select="link"/>
<xsl:copy-of select="description"/>
<xsl:apply-templates>
<xsl:sort select="sort"/>
</xsl:apply-templates>
</channel>
</rss>
</xsl:template>
<xsl:template match="item">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="label">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
的“散兵游勇”是这样的,当一切都说过和做过采用了最新的XSL:
<rss xmlns:st="http://ww2.startribune.com/rss/modules/base/">
<channel>
<title>A Title</title>
<link>http://www.mydomain.com/</link>
<description>The Description</description>
A Title
http://www.mydomain.com/
The Description
<label>...
<item>...
由于默认的匹配模板XSLT引擎提供“零星”。由于您明确处理了通道模板中的标题,链接和描述元素,因此您需要为它们创建emptry模板来吸取文本。您的常规应用模板调用是默认模板被触发的原因。 – ewh 2011-04-23 03:30:35
@ewh - 您应该将您的评论发布为答案,我会赞扬它。 – 2011-04-23 03:37:51
@ewh - 这些空模板是什么样子的?它们在哪里?我了解“apply-templates”对select = item做了什么,并且看看它现在在做什么。我怎么能根据普通的“排序”子节点对标签和项目进行排序? – ropadope 2011-04-23 04:09:47