1

我有 XML(非常大的文件),我想根据属性(列)值的排序获得输出contact_name。这可以使用某些工具或编码吗?

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
<smses count="4">

 <sms address="+381642" subject="null" contact_name="C" />
 <sms address="+3816424" subject="null" contact_name="A" />
 <sms address="+3816427" subject="null" contact_name="B" />
</smses>

短信.xsl 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://android.riteshsahu.com">
<xsl:template match="/">
  <html>
      <head>
          <style type="text/css">
          body 
          {
            font-family:arial,sans-serif;
            color:#000;
            font-size:13px;
            color:#333;
          }
          table 
          {
            font-size:1em;
            margin:0 0 1em;
            border-collapse:collapse;
            border-width:0;
            empty-cells:show;
          }
          td,th 
          {
            border:1px solid #ccc;
            padding:6px 12px;
            text-align:left;
            vertical-align:top;
            background-color:inherit;
          }
          th 
          {
            background-color:#dee8f1;
          }
          </style>
      </head>
      <body>
      <h2>SMS Messages</h2>
      <table>
        <tr>
          <th>Type</th>
          <th>Number</th>
          <th>Contact</th>
          <th>Date</th>
          <th>Message</th>
        </tr>
        <xsl:for-each select="smses/sms">
         <xsl:sort select="contact_name" data-type="text"/>
        <tr>
          <td>
            <xsl:if test="@type = 1">
            Received
            </xsl:if>
            <xsl:if test="@type = 2">
            Sent
            </xsl:if>
          </td>
          <td><xsl:value-of select="@address"/></td>
          <td><xsl:value-of select="@contact_name"/></td>
          <td><xsl:value-of select="@date"/><br/><xsl:value-of select="@readable_date"/></td>
          <td><xsl:value-of select="@body"/></td>
        </tr>
        </xsl:for-each>
      </table>
      </body>
  </html>
</xsl:template>
</xsl:stylesheet>
4

2 回答 2

1

在您的 XSLT 文件中sms.xsl,您可以<xsl:sort>使用 using@来引用属性,如下所示...

<xsl:sort select="@contact_name"/>

注意:定位很重要,它需要在<xsl:for-each>or内<xsl:apply-templates><xsl:for-each>因此,只需在文件的开头之后直接插入上述行sms.xsl...

<xsl:for-each select="smses/sms">
  <xsl:sort select="@contact_name"/> <!-- new line -->
于 2013-05-26T08:44:59.560 回答
0

这是 XML 中排序的语法

<xsl:sort select="expression"
lang="language-code"
data-type="text|number|qname"
order="ascending|descending"
case-order="upper-first|lower-first"/>

在此之前,您需要了解 XSTL 格式。在此处查看示例

其次,您<xsl:sort select="contact_name" data-type="text"/> 在 XML 的 XSL 文件中使用过

终于到了

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://android.riteshsahu.com">
<xsl:template match="/">
  <html>
      <head>
          <style type="text/css">
          body 
          {
            font-family:arial,sans-serif;
            color:#000;
            font-size:13px;
            color:#333;
          }
          table 
          {
            font-size:1em;
            margin:0 0 1em;
            border-collapse:collapse;
            border-width:0;
            empty-cells:show;
          }
          td,th 
          {
            border:1px solid #ccc;
            padding:6px 12px;
            text-align:left;
            vertical-align:top;
            background-color:inherit;
          }
          th 
          {
            background-color:#dee8f1;
          }
          </style>
      </head>
      <body>
      <h2>SMS Messages</h2>
      <table>
        <tr>
          <th>Type</th>
          <th>Number</th>
          <th>Contact</th>
          <th>Date</th>
          <th>Message</th>
        </tr>
        <xsl:for-each select="smses/sms">
        <!-- Sorting added here -->
       <xsl:sort select="contact_name" data-type="text"/>             
        <tr>
          <td>
            <xsl:if test="@type = 1">
            Received
            </xsl:if>
            <xsl:if test="@type = 2">
            Sent
            </xsl:if>
          </td>  
          <td><xsl:value-of select="@address"/></td>
          <td><xsl:value-of select="@contact_name"/></td>
          <td><xsl:value-of select="@date"/><br/><xsl:value-of select="@readable_date"/></td>
          <td><xsl:value-of select="@body"/></td>
        </tr>
        </xsl:for-each>
      </table>
      </body>
  </html>
</xsl:template>
</xsl:stylesheet>
于 2013-05-26T08:56:28.500 回答