xml - XSLT 2.0 - 排序问题

标签 xml sorting xslt xslt-2.0

我有以下输入 XML,并且在使用 XSLT 对数字进行排序时遇到问题。我尝试了多种逻辑但无法生成预期结果。当数据只有一个 AccountNo 时,我可以对数据进行排序,但当记录有多个 AccountNo 时,我会遇到问题并抛出太多排序键值的错误。我想按AccountNo对数据进行排序。

<-------XML Data---------->

<?XML version="1.0" encoding="UTF-8">
<Bank>
    <Customer>
        <Account>
            <AccountNo>999</AccountNo>
            <AccountNo>1004</AccountNo>
            <AccountNo>1002</AccountNo>
        </Account>
        <FirstName>Ramesh</FirstName>
        <LastName>Patel</LastName>
        <ContactNo>1234567890</ContactNo>
    </Customer>
    <Customer>
        <Account>
            <AccountNo>1001</AccountNo>
        </Account>
        <FirstName>Viraj</FirstName>
        <LastName>Shah</LastName>
        <ContactNo>4567890989</ContactNo>
    </Customer>
    <Customer>
        <Account>
            <AccountNo>1003</AccountNo>
            <AccountNo>1005</AccountNo>
        </Account>
        <FirstName>Kapil</FirstName>
        <LastName>Sharma</LastName>
        <ContactNo>3456789320</ContactNo>
    </Customer>
</Bank>

    <---------Expected output------->
    999     Ramesh  Patel  1234567890
    1001    Viraj   Shah   4567890989
    1002    Ramesh  Patel  1234567890
    1003    Kapil   Sharma 3456789320
    1004    Ramesh  Patel  1234567890
    1005    Kapil   Sharma 3456789320

最佳答案

我要做的是将模板应用于所有 AccountNo 元素并对它们进行排序。

然后匹配AccountNo并输出条目...

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <xsl:apply-templates select=".//AccountNo">
      <xsl:sort data-type="number"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="AccountNo">
    <xsl:value-of select="string-join((.,../../(FirstName,LastName,ContactNo)),'&#x9;')"/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

输出

1000    Ramesh  Patel   1234567890
1001    Viraj   Shah    4567890989
1002    Ramesh  Patel   1234567890
1003    Kapil   Sharma  3456789320
1004    Ramesh  Patel   1234567890
1005    Kapil   Sharma  3456789320

工作 fiddle :http://xsltfiddle.liberty-development.net/948Fn5o

更新

由于您的输出应该位于固定字段中,因此我建议创建一个函数,您可以使用该函数用空格填充字符串以适合该字段。这将使您能够保证该字段正是您想要的宽度。

示例...

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:l="local">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <xsl:apply-templates select=".//AccountNo">
      <xsl:sort data-type="number"/>
    </xsl:apply-templates>
  </xsl:template>

  <!--Note: If the string to pad is longer than the width specified, the string
  will be truncated to fit the width.-->
  <xsl:function name="l:pad">
    <xsl:param name="toPad" as="xs:string?"/>
    <xsl:param name="width" as="xs:integer"/>
    <xsl:variable name="padding" 
      select="for $x in 1 to $width - string-length(normalize-space($toPad)) return ' '"/>
    <xsl:value-of 
      select="substring(
      concat(normalize-space($toPad), string-join($padding,'')),
      1,$width)"/>
  </xsl:function>  

  <xsl:template match="AccountNo">
    <xsl:value-of select="(
      l:pad(.,8),
      ../../(
      l:pad(FirstName,8),
      l:pad(LastName,8),
      l:pad(ContactNo,10)
      ),'&#xA;')"/>
  </xsl:template>

</xsl:stylesheet>

更新了 fiddle :http://xsltfiddle.liberty-development.net/948Fn5o/3

关于xml - XSLT 2.0 - 排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49262038/

相关文章:

python - 使用 Py2neo 将大型 xml 文件导入 Neo4j

c++ - 基于 C++ 中的属性对一类对象进行分区的优化方法

c++ - 从另一个 C++ 订购数组

xml - 使用 XSLT 将 XML 中的日期和转换为 UTC 时区

java - 使用java Spring在post方法中使用一个请求主体到不同的xml

java - JBoss 7.1.1中weblogic的Application.xml文件

python - 在这种情况下,sorted() 的替代方法是什么?或者这可以管理吗?

xslt - 在 BizTalk 中映射递归结构

javascript - 在 XSL 中呈现 JavaScript

php - 如何从大于可用 RAM 的 xml 文件中删除 xml 元素/节点?