XSLT排序问题w /包含单数字和双数字文本和数字的字符串

时间:2017-04-27 16:12:46

标签: xml xslt

我正在寻找帮助来解决我遇到的XSLT样式表问题,我正在申请基本的XML文件,它将为我们正在构建的门户输出基本HTML。具体来说,我正在尝试应用< xslt:sort select =“”/>到我输出的内容是基于title元素,其中包括文本和单位和双位数字,sort元素在单个数字之前放置两位数字(参见下面的示例):

示例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

1 个答案:

答案 0 :(得分: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>