我正在使用以下XML天氣傳感器結構。目前的目標是對這種結構進行分類,首先通過傳感器位置,然後通過閱讀標籤。我是一個掙扎的XML處理新手!使用XSL轉換將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文件。大部分工作已經開始,除了排序過程。