Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XSLT对IP地址进行排序_Xml_Sorting_Xslt - Fatal编程技术网

Xml 使用XSLT对IP地址进行排序

Xml 使用XSLT对IP地址进行排序,xml,sorting,xslt,Xml,Sorting,Xslt,我试图对通过PHP解析的CSV文件中的数据进行排序。PHP获取数据并将其转换为XML,然后由XSL样式表格式化。数据包含IP地址、日期和用户代理。XSL按IP地址对数据进行分组,我还需要它按IP地址进行排序 以下是我的XSL代码: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

我试图对通过PHP解析的CSV文件中的数据进行排序。PHP获取数据并将其转换为XML,然后由XSL样式表格式化。数据包含IP地址、日期和用户代理。XSL按IP地址对数据进行分组,我还需要它按IP地址进行排序

以下是我的XSL代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="group-by-ipaddress" match="row" use="ipaddress" />
    <xsl:template match="rows">
        <html>
            <body>

                <xsl:for-each select="row[count(. | key('group-by-ipaddress', ipaddress)[1]) = 1]">
                        <xsl:sort select="ipaddress"/>

                            <p>IP Address: <xsl:value-of select="ipaddress"/></p>
                                <xsl:for-each select="key('group-by-ipaddress', ipaddress)">
                                    <xsl:sort select="date" />
                                <p><blockquote>Date: <xsl:value-of select="date"/> : <xsl:value-of select="userAgent"/></blockquote></p>

                        </xsl:for-each>
                    </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
IP地址:123.33.44.55

Date: 2013-01-03 : Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0

Date: 2013-01-03 : Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Date: 2013-01-06 : Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Date: 2013-01-07 : Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
IP地址:212.38.44.55

Date: 2013-01-02 : Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)

Date: 2013-01-06 : Mozilla/3.0 (x86 [en] Windows NT 5.1; Sun)
IP地址:256.19.44.55

Date: 2013-01-06 : Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
IP地址:5.255.23.25

Date: 2013-02-02 : Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)

Date: 2013-02-05 : Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)

您正在对此处的IP地址进行alpha排序,并在
5.255.23.25
之前按字母顺序排列
12.22.333.44
。如果您想正确地对它们进行排序,它就不会这么漂亮了:

<xsl:for-each select="row[count(. | key('group-by-ipaddress', ipaddress)[1]) = 1]">
  <xsl:sort select="substring-before(ipaddress, '.')" 
            data-type="number"/>
  <xsl:sort select="substring-before(
                    substring-after(ipaddress, '.'), '.')" 
            data-type="number"/>
  <xsl:sort select="substring-before(
                    substring-after(
                    substring-after(ipaddress, '.'), '.'), '.')" 
            data-type="number"/>
  <xsl:sort select="substring-after(
                    substring-after(
                    substring-after(ipaddress, '.'), '.'), '.')" 
            data-type="number"/>

  <!-- Contents of for-each -->

</xsl:for-each>

<xsl:for-each select="row[count(. | key('group-by-ipaddress', ipaddress)[1]) = 1]">
  <xsl:sort select="substring-before(ipaddress, '.')" 
            data-type="number"/>
  <xsl:sort select="substring-before(
                    substring-after(ipaddress, '.'), '.')" 
            data-type="number"/>
  <xsl:sort select="substring-before(
                    substring-after(
                    substring-after(ipaddress, '.'), '.'), '.')" 
            data-type="number"/>
  <xsl:sort select="substring-after(
                    substring-after(
                    substring-after(ipaddress, '.'), '.'), '.')" 
            data-type="number"/>

  <!-- Contents of for-each -->

</xsl:for-each>