XSLT排序区分大小写

时间:2016-08-16 09:50:23

标签: xml xslt

我有以下XML:

<catalog>
    <cd>
        <Title>Empire Burlesque</Title>
        <artist>Bob Dylan</artist>
        <company>Columbia</company>
        <cOuntry>USA</cOuntry>
        <price>10.90</price>
        <year>1985</year>
    </cd>
</catalog>

我想订购&#34; cd&#34;元素的名称,而不是内容,我希望排序区分大小写,首先是大写。我尝试了以下XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="catalog/cd">
        <xsl:copy>
            <xsl:apply-templates>
                <xsl:sort select="name()" case-order="upper-first" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

我希望得到以下结果&#34; Title&#34;首先是因为大写的T和&#34; cOuntry&#34;来自&#34;公司&#34;。

<catalog>
   <cd>
      <Title>Empire Burlesque</Title>
      <artist>Bob Dylan</artist>
      <cOuntry>USA</cOuntry>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
</catalog>

但我得到以下结果:

<catalog>
   <cd>
      <artist>Bob Dylan</artist>
      <company>Columbia</company>
      <cOuntry>USA</cOuntry>
      <price>10.90</price>
      <Title>Empire Burlesque</Title>
      <year>1985</year>
   </cd>
</catalog>

根据http://www.dpawson.co.uk/xsl/sect2/N6461.html#d9252e1397&#34;仅在两个字符串(整体)相同的情况下才考虑情况,除非情况为&#34;。

我希望首先考虑案例。我如何排序它确实区分大小写?

在线测试:http://xsltransform.net/bFWR5DG

2 个答案:

答案 0 :(得分:1)

您的在线工具使用XSLT 2.0处理器Saxon 9,假设您可以使用XSLT 2.0

<xsl:template match="catalog/cd">
    <xsl:copy>
        <xsl:apply-templates>
            <xsl:sort select="name()" collation="http://www.w3.org/2005/xpath-functions/collation/codepoint" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

并获得订单

<catalog>
   <cd>
      <Title>Empire Burlesque</Title>
      <artist>Bob Dylan</artist>
      <cOuntry>USA</cOuntry>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
</catalog>

答案 1 :(得分:0)

我最终使用了此处的解决方案:Sort given order with xslt sort

<xsl:variable name="sortOrder" select="'|Title|artist|cOuntry|company|price|year|'" />
<xsl:template match="catalog/cd">
    <xsl:copy>
        <xsl:apply-templates>
            <xsl:sort data-type="number" 
                select="string-length(substring-before($sortOrder, concat('|', name(), '|')))" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>
相关问题