繁体   English   中英

协助XSL转换xml文件

[英]assistance with xsl transform of a xml file

首先..我完全是这个新手,而不是专业程序员..通常我会制作小型的php网站。

我有一个水冷却系统。 Watercoolingsystem有一个xml输出,它将所有收集的数据写入xml文件,每隔x秒刷新一次。

现在,我还有一个配有LCDhost软件的G19 LCD键盘。该软件可以读取XML文件并显示值。

不幸的是,LCDhost可以解释Watercooling xml ..我必须重建文件。

我开始四处看看,但是我在XML编码方面的技能是-100 :(

File 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>

所以我删除了不需要的行..但是我不知道如何重命名。 可以帮助我恢复xml ..我需要从每个LogDataSet中删除所有其他节点的名称和值。

由此:

  <value>29.68</value>
  <name>aquaero CPU</name>

对此:

<aquaero CPU>29.68</aquaero CPU>

先感谢您

麦克风

该模板可以帮助您入门:

几件事要考虑:

  • XML元素名称不能包含空格,因此我们需要替换它们
  • XML元素名称不能包含%之类的字符,因此我必须将其替换为翻译功能

     <?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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM