首先......我完全是新手,而不是专业编码人员...通常我会制作小型php网站。
我有一个水冷系统。 Watercoolingsystem有一个xml输出,它将所有收集的数据写入xml文件中,每隔x秒刷新一次。
现在我还有一个用LCDhost软件驱动的G19 LCD键盘。该软件可以读取XML文件并显示值。
不幸的是,LCDhost可以解释Watercooling xml ..我必须重建文件。
我开始环顾四周,但我的XML编码技巧是-100 :(
文件a看起来如此:
<?xml version="1.0" encoding="utf-8"?>
<LogDataExport xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Aquasite2012</name>
<exportTime>2013-04-04T21:00:20.7286335+02:00</exportTime>
<logdata>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>24.72</value>
<name>Sensor 1</name>
<unit>°C</unit>
<valueType>temperature</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>46.63</value>
<name>Sensor 2</name>
<unit>°C</unit>
<valueType>temperature</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>29.68</value>
<name>aquaero CPU</name>
<unit>°C</unit>
<valueType>temperature</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>161</value>
<name>Flow 1</name>
<unit>l/h</unit>
<valueType>flow</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>149.7</value>
<name>Flow 2</name>
<unit>l/h</unit>
<valueType>flow</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>870</value>
<name>Fan 2</name>
<unit>rpm</unit>
<valueType>rpm</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>860</value>
<name>Fan 3</name>
<unit>rpm</unit>
<valueType>rpm</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.650</t>
<value>956</value>
<name>Fan 4</name>
<unit>rpm</unit>
<valueType>rpm</valueType>
<device>aquaero 5</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.363</t>
<value>80</value>
<name>Füllstand in %</name>
<unit>%</unit>
<valueType>percent</valueType>
<device>tubemeter</device>
</LogDataSet>
<LogDataSet>
<t>2013-04-04T21:00:20.164</t>
<value>26.6</value>
<name>Wassertemperatur</name>
<unit>°C</unit>
<valueType>temperature</valueType>
<device>aquastream xt</device>
</LogDataSet>
</logdata>
</LogDataExport>
我需要这样的输出:
<as2012>
<LogData>
<Date>03.04.2013</Date>
<Time>17:03:39</Time>
<Sensor1>19,90</Sensor1>
<Sensor2>19,90</Sensor2>
<Sensor3>---,--</Sensor3>
<Sensor4>18,90</Sensor4>
<Sensor5>21,90</Sensor5>
<Sensor6>21,60</Sensor6>
<Fan1Rpm>845</Fan1Rpm>
<Fan2Rpm>939</Fan2Rpm>
<Fan3Rpm>863</Fan3Rpm>
<Fan4Rpm>16640</Fan4Rpm>
<Fan1Power>100,00</Fan1Power>
<Fan2Power>100,00</Fan2Power>
<Fan3Power>100,00</Fan3Power>
<Fan4Power>100,00</Fan4Power>
<Led1Power>0,00</Led1Power>
<Led2Power>0,00</Led2Power>
<Flow1Hour>160,8300</Flow1Hour>
<Flow1Minute>2,6805</Flow1Minute>
<Flow2Hour>166,4000</Flow2Hour>
<Flow2Minute>2,7733</Flow2Minute>
<TubemeterLevel>255,00</TubemeterLevel>
<TubemeterWave>255,00</TubemeterWave>
<PowerSensor1>0,00</PowerSensor1>
<PowerSensor2>0,00</PowerSensor2>
<Aquastream1Voltage>0,0</Aquastream1Voltage>
<Aquastream1Current>0</Aquastream1Current>
<Aquastream1Power>0,0</Aquastream1Power>
<Aquastream1Frequence>120</Aquastream1Frequence>
<Aquastream2Voltage>0,0</Aquastream2Voltage>
<Aquastream2Current>0</Aquastream2Current>
<Aquastream2Power>0,0</Aquastream2Power>
<Aquastream2Frequence>120</Aquastream2Frequence>
</aquaero4LogData>
</aquaero4LogData>
我正试图找到一种方法,但我的知识很低..目前我有这个:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" version="1.0" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="t">
<xsl:for-each select="t">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="unit">
<xsl:for-each select="unit">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="valueType">
<xsl:for-each select="valueType">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="device">
<xsl:for-each select="device">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="value">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
所以我删除了我不需要的行..但我不知道如何重命名。 someony可以帮我重新构建xml ..我需要从每个LogDataSet中删除名称和值,然后删除其他节点。
来自:
<value>29.68</value>
<name>aquaero CPU</name>
到此:
<aquaero CPU>29.68</aquaero CPU>
提前谢谢
MIC
答案 0 :(得分:0)
此模板可以帮助您入门:
很少有事情需要考虑:
XML元素名称不能包含%等字符,因此我必须将其替换为translate函数
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="LogDataExport/logdata"/>
</xsl:template>
<!-- Build your output file, set date and time and then recurse the rest
of input doc -->
<xsl:template match="logdata">
<xsl:copy>
<xsl:element name="Date">
<!-- Grab the Date/Time from the first LogDataSet/t element -->
<xsl:value-of select="substring-before(LogDataSet[1]/t,'T')"/>
</xsl:element>
<xsl:element name="Time">
<xsl:value-of select="substring-after(LogDataSet[1]/t,'T')"/>
</xsl:element>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- For each name element in LogDataSet create an element with that value
and set the value to the content of the value element -->
<xsl:template match="LogDataSet">
<!-- remove spaces and % symbol from name value -->
<xsl:element name="{translate(translate(name,' ',''),'%','P')}">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:template>
<!-- Identity template recurse input document elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>