XSLT不会按xml字段按字母顺序排序
问题描述:
我试图按“ownerName”中的姓氏字段对此数据进行排序。这种排序代码是否在正确的位置?当我尝试运行它时,它给了我一个解析错误。XSLT不会按xml字段按字母顺序排序
<xsl:apply-templates select="storeloation" /><br/>
<xsl:apply-templates select="storeURL" /><br/>
<xsl:apply-templates select="storeDescription" /><br/>
<xsl:apply-templates select="related stores" /><br/>
<xsl:apply-templates select="storecustomerCount" /><br/>
<xsl:apply-templates select="storeVisits" /><br/>
<xsl:apply-templates select="storeestablished" /><br/>
<xsl:apply-templates select="ownersProfile/ownersName/firstName"/>
<xsl:apply-templates select="ownersProfile/ownersName/surname"/>
<xsl:sort select="surname" /><br/>
<xsl:apply-templates select="ownersProfile/ownersEmail"/><br/>
<xsl:apply-templates select="ownersProfile/ownersAddress"/><br/>
如果我没有记错的话(我大概是)这种说法有短排序由业主的姓氏模板匹配的每个实例?
答
这里有两个错误。
首先,解析错误是因为xsl:sort应该是xsl:apply-templates的子项,而不是兄弟。
其次,对xsl:sort
的选择表达式进行评估,序列中的每个项目被排序为上下文节点。这些项目在你的情况下是surname
元素。我怀疑surname
元素没有名为surname
的子元素;而你想要的是<xsl:sort select="."/>
。
所以,正确的形式是:
<xsl:apply-templates select="ownersProfile/ownersName/surname">
<xsl:sort select="." />
</xsl:apply-templates>
<br/>
顺便说一句,我们大多数人找到,如果你告诉我们什么错误是它更容易诊断“解析错误”。说没有说失败是失败的就像告诉你的医生你痛苦而不说疼痛在哪里。
==细想... ==
您还没有表现出你的XML数据源,但是想什么元素名称可能意味着,我怀疑有多个ownersProfile元素,每一个ownersName,其中有一个或多个firstName和一个姓氏,以及其他属性,如电子邮件地址。在这种情况下,你不想排序姓氏,你想排序的配置文件。所以它变成这样的:
<xsl:for-each select="ownersProfile">
<xsl:sort select="ownersName/surname"/>
<xsl:apply-templates select="ownersName/firstName"/>
<xsl:apply-templates select="ownersName/surname"/>
<xsl:apply-templates select="ownersEmail"/><br/>
<xsl:apply-templates select="ownersAddress"/><br/>
...
</xsl:for-each>
但是我现在完全进入了猜测的境界。