0

在尝试对输入 XML 进行排序时,我遇到了一种奇怪的行为:

我的 XSL:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:output method="xml" indent="yes"/>
<!--  <xsl:strip-space elements="*"/>  -->

 <xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="name">
     <xsl:sort select="@rank" data-type="number"/>   
     <xsl:sort collation = "http://www.w3.org/2005/xpath-functions/collation/codepoint"/>    
    </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="name">
  <name rank="{@rank}">
    <xsl:copy-of select="text()"/>
    <xsl:apply-templates select="name">
    <xsl:sort select="@rank" data-type="number"/>    
    <xsl:sort collation = "http://www.w3.org/2005/xpath-functions/collation/codepoint" />    
    </xsl:apply-templates>
  </name>
 </xsl:template>
</xsl:stylesheet>

当我的输入 XML 是以下形式时:

<?xml version="1.0" encoding="UTF-8"?>
<Sources>
<name rank="">Continents / Regions (energy)</name>
<name rank="">Continents / Regions</name>
</Sources>

它被正确排序为:

<?xml version="1.0" encoding="UTF-8"?>
    <Sources>
    <name rank="">Continents / Regions</name>
    <name rank="">Continents / Regions (energy)</name>    
    </Sources>

但是,当输入为:

<?xml version="1.0" encoding="UTF-8"?>
<Sources>
<name rank="">Continents / Regions (energy)
  <name rank="">ABC</name>
</name>
<name rank="">Continents / Regions
  <name rank="">ABC</name>
</name>
</Sources>

输出不正确:

<?xml version="1.0" encoding="UTF-8"?>
<Sources>
<name rank="">Continents / Regions (energy)
  <name rank="">ABC</name>
</name>
<name rank="">Continents / Regions
  <name rank="">ABC</name>
</name>
</Sources>

如果有人能给我指点我应该看什么,我将不胜感激。提前致谢!

编辑:正在使用的 XSLT 处理器是 Saxon HE 9.4 。这是我的 Java 代码。

tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);

Transformer transformer = tFactory.newTransformer(new StreamSource(RCSTestDriver.TestDataPath + "/transform.xslt")); 
4

2 回答 2

0

When I try to reproduce the problem with the input document being

<?xml version="1.0" encoding="UTF-8"?>
<Sources>
<name>Continents / Regions (energy)
  <name>ABC</name>
</name>
<name>Continents / Regions
  <name>ABC</name>
</name>
</Sources>

the stylesheet as posted and the XSLT 2.0 processor Saxon 9.4 HE run from the command line I can't reproduce it, instead I get the output

<?xml version="1.0" encoding="UTF-8"?>
<Sources>
   <name rank="">Continents / Regions

<name rank="">ABC</name>
   </name>
   <name rank="">Continents / Regions (energy)

<name rank="">ABC</name>
   </name>
</Sources>
于 2013-01-02T17:18:12.983 回答
0

这取决于有多少空白(这很难从您的帖子中看出)。但是字符串值“Continents / Regions ABC”当然应该在“Continents / Regions(energy)ABC”之前,因为“(”在“A”之前。也许您忽略了“ABC”是排序键的一部分?

于 2013-01-02T18:25:09.747 回答