我正在使用以下的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格式呈现,与输入文件完全相同,但按传感器“位置”和读取“标签”按A-Z顺序排序。 经过大量的阅读和谷歌搜索,这就是我的成果:
<?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>
我正在使用微软XML编辑器测试我的代码。首先,输出似乎没有输入文件的XML结构,而且它也不完整,传感器位置和其他级别及以上的项目都缺失了。
我做错了什么?
顺便说一下,在我解决这个问题之后,我将使用经典ASP中的XSL转换文件处理完整的XML文件。大部分已经工作正常,除了排序过程。