【问题标题】:XSLT Sorting Issue w/ String Containing Text and Numbers That Are Single and Double DigitXSLT 排序问题,字符串包含单个和两位数的文本和数字
【发布时间】:2017-04-27 16:12:46
【问题描述】:

我正在寻求帮助来解决我遇到的 XSLT 样式表问题,我将其应用于基本 XML 文件,该文件将为我们正在构建的门户输出基本 HTML。具体来说,我正在尝试将 应用于我基于标题元素输出的内容,其中包括文本以及一位数和两位数,并且排序元素将两位数放在单数之前数字(见下面的例子):

示例 XML 源文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <TitleList>
    <book>
        <title>Book 1: Part 3. Buying and Selling Stuff</title>
        <author>Jon Doe</author>
        <PubDate>2010</PubDate>
     </book>
     <book>
      <title>Book 1: Part 7. Making Stuff</title>
      <author>Jane Doe</author>
      <PubDate>2012</PubDate>
      </book>
      <book>
        <title>Book 1: Part 8. The Art of Creating Things </title>
        <author>Jon Doe</author>
        <PubDate>2013</PubDate>
      </book>
     <book>
      <title>Book 1: Part 10. Stuff Happens</title>
      <author>Jane Doe</author>
      <PubDate>2014</PubDate>
     </book>
     </TitleList>

XSLT 示例:

     <?xml version="1.0" encoding="UTF-8"?>
     <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
       exclude-result-prefixes="xs"
       version="1.0">
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/">
    <html>
        <head></head>
        <body>
           <div id="titleList">
               <table>
                   <tr>
                       <th>Title List</th>
                   </tr>
                   <tr>
                       <xsl:call-template name="retrieveTitles"></xsl:call-template>
                   </tr>
               </table>
           </div> 
        </body>
    </html>
</xsl:template>
<xsl:template name="retrieveTitles">
    <xsl:for-each select="TitleList/book">
     <xsl:sort select="title" /> 
     <tr>   
      <td>  
        <xsl:value-of select="title"/>
      </td>
        <td>
            <xsl:value-of select="author" />
        </td>
        <td>
            <xsl:value-of select="PubDate"/>
        </td>
     </tr>   
    </xsl:for-each>
  </xsl:template>
  </xsl:stylesheet>

标题乱序的结果 HTML:

    <html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     </head>
     <body>
     <div id="titleList">
     <table>
        <tr>
           <th>Title List</th>
        </tr>
        <tr>
           <tr>
              <td>Book 1: Part 10. Stuff Happens</td>
              <td>Jane Doe</td>
              <td>2014</td>
           </tr>
           <tr>
              <td>Book 1: Part 3. Buying and Selling Stuff</td>
              <td>Jon Doe</td>
              <td>2010</td>
           </tr>
           <tr>
              <td>Book 1: Part 7. Making Stuff</td>
              <td>Jane Doe</td>
              <td>2012</td>
           </tr>
           <tr>
              <td>Book 1: Part 8. The Art of Creating Things </td>
              <td>Jon Doe</td>
              <td>2013</td>
           </tr>
        </tr>
     </table>
  </div>
    </body>
   </html>  

如何配置 XSLT 脚本的排序元素,以确保在从一位数跳转到两位数时,标题是连续排序的?感谢您的宝贵时间!

问候,

wyattburp86

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    为了尽量减少手头问题的示例,请考虑以下几点:

    <xsl:template match="/TitleList">
        <table>
            <xsl:apply-templates select="book">
                <xsl:sort select="substring-before(substring-after(title, 'Book ' ), ':')" data-type="number" order="ascending"/>
                <xsl:sort select="substring-before(substring-after(title, 'Part ' ), '.')" data-type="number" order="ascending"/>
            </xsl:apply-templates>
        </table>
    </xsl:template>
    
    <xsl:template match="book">
        <tr>
            <td>  
                <xsl:value-of select="title"/>
            </td>
        </tr>       
    </xsl:template>
    

    【讨论】:

    • 成功了!我将您提供的排序选择功能合并到我的环境中,并且排序正确。我很高兴您的建议包括“substring-before”,因为我最初正在查看它,但是您帮助填写了我缺少的部分!非常感谢 - 干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2013-11-03
    • 2016-10-28
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多