3

我正在使用以下 XML、天气传感器结构。近期的目标是对这个结构进行排序,首先是传感器位置,然后是读取标签。我是一个在 XML 处理方面苦苦挣扎的新手!!!

<Sensors>
    <sensor location="House - Front Entry">
        <reading label="Temperature">
            <title>House - Front Entry</title>
            <label>Temperature</label>
            <value>57</value>
            <units>F</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>64</hi>
                    <lo>56</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>72</hi>
                    <lo>46</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>79</hi>
                    <lo>46</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>80</hi>
                    <lo>41</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>80</hi>
                    <lo>41</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>House - Front Entry</title>
            <label>Humidity</label>
            <value>83</value>
            <units>%</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>83</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>83</hi>
                    <lo>42</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
            </records>
        </reading>
    </sensor>
    <sensor location="House - Patio">
        <reading label="Temperature">
            <title>House - Patio</title>
            <label>Temperature</label>
            <value>53</value>
            <units>F</units>
            <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>65</hi>
                    <lo>51</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>71</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>House - Patio</title>
            <label>Humidity</label>
            <value>98</value>
            <units>%</units>
            <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>99</hi>
                    <lo>47</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>99</hi>
                    <lo>33</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
            </records>
        </reading>
        <reading label="Rainfall">
            <title>House - Patio</title>
            <label>Rainfall</label>
            <value>0.22</value>
            <units>in</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Previous Event">
                    <timeFrame>Previous Event</timeFrame>
                    <total>0.24</total>
                    <eventDate>05/22/2013</eventDate>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <total>0.22</total>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <total>6.97</total>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <total>6.97</total>
                </record>
            </records>
        </reading>
    </sensor>

    <sensor location="Cabin - Kitchen">
        <reading label="Temperature">
            <title>Cabin - Kitchen</title>
            <label>Temperature</label>
            <value>57</value>
            <units>F</units>
            <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>63</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>74</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>74</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>76</hi>
                    <lo>30</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>76</hi>
                    <lo>30</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>Cabin - Kitchen</title>
            <label>Humidity</label>
            <value>41</value>
            <units>%</units>
            <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>41</hi>
                    <lo>40</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>48</hi>
                    <lo>40</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>48</hi>
                    <lo>39</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>48</hi>
                    <lo>13</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>48</hi>
                    <lo>13</lo>
                </record>
            </records>
        </reading>
    </sensor>
</Sensors>

好的,上面是 XLM,下面是我创建 XSL 转换文件的最佳尝试。我希望结果输出采用 XML 格式,与输入文件完全相同,但按传感器“位置”按 AZ 顺序排序并读取“标签”。经过大量阅读和谷歌搜索,这就是我想出的:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="Sensors">
      <xsl:copy>    
    <xsl:apply-templates>
      <xsl:sort select="sensor/@location"/>
      <xsl:sort select="reading/label"/>
    </xsl:apply-templates>
      </xsl:copy>     
  </xsl:template>

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

我正在使用 Microsoft XML 编辑器来测试我的代码。首先输出似乎没有输入文件的XML结构,也不完整,缺少传感器位置和其他级别及以上的项目。

我究竟做错了什么?

顺便说一句,在我弄清楚这一点后,我将使用经典 ASP 中的 XSL 转换文件来处理完整的 XML 文件。其中大部分已经在工作,当然除了排序过程。

4

2 回答 2

1

从身份转换开始,然后覆盖要排序的元素的父级。您可以使用sortinxsl:apply-templates进行排序。

XML 输入

<Sensors>
    <sensor location="House - Front Entry">
        <reading label="Temperature">
            <title>House - Front Entry</title>
            <label>Temperature</label>
            <value>57</value>
            <units>F</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>64</hi>
                    <lo>56</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>72</hi>
                    <lo>46</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>79</hi>
                    <lo>46</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>80</hi>
                    <lo>41</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>80</hi>
                    <lo>41</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>House - Front Entry</title>
            <label>Humidity</label>
            <value>83</value>
            <units>%</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>83</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>83</hi>
                    <lo>42</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>99</hi>
                    <lo>31</lo>
                </record>
            </records>
        </reading>
    </sensor>
    <sensor location="House - Patio">
        <reading label="Temperature">
            <title>House - Patio</title>
            <label>Temperature</label>
            <value>53</value>
            <units>F</units>
            <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>65</hi>
                    <lo>51</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>71</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>80</hi>
                    <lo>38</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>House - Patio</title>
            <label>Humidity</label>
            <value>98</value>
            <units>%</units>
            <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>99</hi>
                    <lo>47</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>99</hi>
                    <lo>33</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>99</hi>
                    <lo>24</lo>
                </record>
            </records>
        </reading>
        <reading label="Rainfall">
            <title>House - Patio</title>
            <label>Rainfall</label>
            <value>0.22</value>
            <units>in</units>
            <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
            <records>
                <record timeFrame="Previous Event">
                    <timeFrame>Previous Event</timeFrame>
                    <total>0.24</total>
                    <eventDate>05/22/2013</eventDate>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <total>0.22</total>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <total>6.97</total>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <total>6.97</total>
                </record>
            </records>
        </reading>
    </sensor>

    <sensor location="Cabin - Kitchen">
        <reading label="Temperature">
            <title>Cabin - Kitchen</title>
            <label>Temperature</label>
            <value>57</value>
            <units>F</units>
            <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>63</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>74</hi>
                    <lo>58</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>74</hi>
                    <lo>38</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>76</hi>
                    <lo>30</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>76</hi>
                    <lo>30</lo>
                </record>
            </records>
        </reading>
        <reading label="Humidity">
            <title>Cabin - Kitchen</title>
            <label>Humidity</label>
            <value>41</value>
            <units>%</units>
            <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
            <records>
                <record timeFrame="Today">
                    <timeFrame>Today</timeFrame>
                    <hi>41</hi>
                    <lo>40</lo>
                </record>
                <record timeFrame="Week">
                    <timeFrame>Week</timeFrame>
                    <hi>48</hi>
                    <lo>40</lo>
                </record>
                <record timeFrame="Month">
                    <timeFrame>Month</timeFrame>
                    <hi>48</hi>
                    <lo>39</lo>
                </record>
                <record timeFrame="Year">
                    <timeFrame>Year</timeFrame>
                    <hi>48</hi>
                    <lo>13</lo>
                </record>
                <record timeFrame="Alltime">
                    <timeFrame>Alltime</timeFrame>
                    <hi>48</hi>
                    <lo>13</lo>
                </record>
            </records>
        </reading>
    </sensor>
</Sensors>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/Sensors">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()">
                <xsl:sort select="@location"/>
            </xsl:apply-templates>
        </xsl:copy>     
    </xsl:template>

    <xsl:template match="sensor">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()">
                <xsl:sort select="@label"/>
            </xsl:apply-templates>
        </xsl:copy>     
    </xsl:template>

</xsl:stylesheet>

XML 输出

<Sensors>
   <sensor location="Cabin - Kitchen">
      <reading label="Humidity">
         <title>Cabin - Kitchen</title>
         <label>Humidity</label>
         <value>41</value>
         <units>%</units>
         <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>41</hi>
               <lo>40</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>48</hi>
               <lo>40</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>48</hi>
               <lo>39</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>48</hi>
               <lo>13</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>48</hi>
               <lo>13</lo>
            </record>
         </records>
      </reading>
      <reading label="Temperature">
         <title>Cabin - Kitchen</title>
         <label>Temperature</label>
         <value>57</value>
         <units>F</units>
         <lastUpdate>05/28/2013 11:29 AM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>63</hi>
               <lo>58</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>74</hi>
               <lo>58</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>74</hi>
               <lo>38</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>76</hi>
               <lo>30</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>76</hi>
               <lo>30</lo>
            </record>
         </records>
      </reading>
   </sensor>
   <sensor location="House - Front Entry">
      <reading label="Humidity">
         <title>House - Front Entry</title>
         <label>Humidity</label>
         <value>83</value>
         <units>%</units>
         <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>83</hi>
               <lo>58</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>83</hi>
               <lo>42</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>99</hi>
               <lo>31</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>99</hi>
               <lo>31</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>99</hi>
               <lo>31</lo>
            </record>
         </records>
      </reading>
      <reading label="Temperature">
         <title>House - Front Entry</title>
         <label>Temperature</label>
         <value>57</value>
         <units>F</units>
         <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>64</hi>
               <lo>56</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>72</hi>
               <lo>46</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>79</hi>
               <lo>46</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>80</hi>
               <lo>41</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>80</hi>
               <lo>41</lo>
            </record>
         </records>
      </reading>
   </sensor>
   <sensor location="House - Patio">
      <reading label="Humidity">
         <title>House - Patio</title>
         <label>Humidity</label>
         <value>98</value>
         <units>%</units>
         <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>99</hi>
               <lo>47</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>99</hi>
               <lo>33</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>99</hi>
               <lo>24</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>99</hi>
               <lo>24</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>99</hi>
               <lo>24</lo>
            </record>
         </records>
      </reading>
      <reading label="Rainfall">
         <title>House - Patio</title>
         <label>Rainfall</label>
         <value>0.22</value>
         <units>in</units>
         <lastUpdate>05/28/2013 09:49 PM</lastUpdate>
         <records>
            <record timeFrame="Previous Event">
               <timeFrame>Previous Event</timeFrame>
               <total>0.24</total>
               <eventDate>05/22/2013</eventDate>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <total>0.22</total>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <total>6.97</total>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <total>6.97</total>
            </record>
         </records>
      </reading>
      <reading label="Temperature">
         <title>House - Patio</title>
         <label>Temperature</label>
         <value>53</value>
         <units>F</units>
         <lastUpdate>05/28/2013 09:48 PM</lastUpdate>
         <records>
            <record timeFrame="Today">
               <timeFrame>Today</timeFrame>
               <hi>65</hi>
               <lo>51</lo>
            </record>
            <record timeFrame="Week">
               <timeFrame>Week</timeFrame>
               <hi>71</hi>
               <lo>38</lo>
            </record>
            <record timeFrame="Month">
               <timeFrame>Month</timeFrame>
               <hi>80</hi>
               <lo>38</lo>
            </record>
            <record timeFrame="Year">
               <timeFrame>Year</timeFrame>
               <hi>80</hi>
               <lo>38</lo>
            </record>
            <record timeFrame="Alltime">
               <timeFrame>Alltime</timeFrame>
               <hi>80</hi>
               <lo>38</lo>
            </record>
         </records>
      </reading>
   </sensor>
</Sensors>
于 2013-06-18T15:55:52.437 回答
1

尝试这样的事情:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/Sensors" >
        <xsl:copy>
            <xsl:apply-templates select="sensor">
                <xsl:sort select="@location"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="sensor">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="reading">
                <xsl:sort select="@label"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2013-06-18T15:57:28.997 回答