代码之家  ›  专栏  ›  技术社区  ›  ar1991

XSLT 2.0-排序问题

  •  2
  • ar1991  · 技术社区  · 6 年前

    我在下面输入了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
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Daniel Haley    6 年前

    我要做的是将模板应用于所有 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
    

    工作小提琴: 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>
    

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