XSLT 2.0-排序问题 [英] XSLT 2.0 - Sorting issue

查看:106
本文介绍了XSLT 2.0-排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have below input XML and I am facing issue while sorting number using XSLT. I have tried multiple logic but not able to generate expected result. I am able to sort the data when it is having only one AccountNo but facing issue when record is having multiple AccountNo and throwing error that too many sort key values. I want to sort the data by 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元素并对其进行排序.

What I would do is apply-templates to all AccountNo elements and sort them.

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

Then match AccountNo and output the entry...

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

正在工作的小提琴: http://xsltfiddle.liberty-development.net/948Fn5o

更新

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

Since your output is supposed to be in a fixed field, I'd suggest creating a function that you can use to pad a string with spaces to fit the field. This will allow you to guarantee that the field is exactly the width you want.

示例...

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>

更新的提琴: http://xsltfiddle.liberty-development.net/948Fn5o/3

这篇关于XSLT 2.0-排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆