Frage

I've lot's of file's like this:

<?xml version="1.0" encoding="utf-8"?>
<DATA>
  <TP ID="1P3">
    <VALUES><DATETIME>2014-04-30T10:14:00Z</DATETIME><EE>4.820</EE><STATUS>0</STATUS></VALUES>
    <VALUES><DATETIME>2014-04-30T10:29:00Z</DATETIME><EE>4.825</EE><STATUS>0</STATUS></VALUES>
    <VALUES><DATETIME>2014-04-30T10:44:00Z</DATETIME><EE>4.831</EE><STATUS>0</STATUS></VALUES>
  </TP>
  <TP ID="4P2">
    <VALUES><DATETIME>2014-04-30T10:14:02Z</DATETIME><EE>556.006</EE><STATUS>0</STATUS></VALUES>
    <VALUES><DATETIME>2014-04-30T10:29:01Z</DATETIME><EE>556.550</EE><STATUS>0</STATUS></VALUES>
    <VALUES><DATETIME>2014-04-30T10:44:01Z</DATETIME><EE>557.097</EE><STATUS>0</STATUS></VALUES>
  </TP>
  <ID>WSE_KA-CCE</ID>
  <SN>140252702523</SN>
  <IP>37.80.230.61</IP>
</DATA>

I need to convert those to CSV, so I created a XSL template, but can't make it work as expected:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
  <xsl:template match="/">
    <xsl:text>Zählernummer,Zeitstempel,Zählerstand
    </xsl:text>
    <xsl:for-each select="//TP">
        <xsl:value-of select="@ID"/>,
    </xsl:for-each>
    <xsl:for-each select="//TP/VALUES">
        <xsl:value-of select="DATETIME"/>,<xsl:value-of select="EE"/><xsl:text>
        </xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

What I want to see is:

Zählernummer,Zeitstempel,Zählerstand
1P3,2014-04-30T10:14:00Z,4.820
1P3,2014-04-30T10:29:00Z,4.825
1P3,2014-04-30T10:44:00Z,4.831
4P2,2014-04-30T10:14:02Z,556.006
4P2,2014-04-30T10:29:01Z,556.550
4P2,2014-04-30T10:44:01Z,557.097

But what I get is this:

212$ xsltproc data_XML2CSV.xml data-3.xml
Zählernummer,Zeitstempel,Zählerstand
    1P3,
    4P2,
    2014-04-30T10:14:00Z,4.820
        2014-04-30T10:29:00Z,4.825
        2014-04-30T10:44:00Z,4.831
        2014-04-30T10:14:02Z,556.006
        2014-04-30T10:29:01Z,556.550
        2014-04-30T10:44:01Z,557.097

This is my first XSL experience and I'm slightly lost, I know I'm nearly there but can't see it...

War es hilfreich?

Lösung

I think you want:

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

    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="/">
        <xsl:text>Zählernummer,Zeitstempel,Zählerstand</xsl:text>
        <xsl:text>&#xA;</xsl:text>
        <xsl:for-each select="DATA/TP/VALUES">
            <xsl:value-of select="../@ID"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="DATETIME"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="EE"/>
            <xsl:text>&#xA;</xsl:text>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

Andere Tipps

You may use the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <xsl:text>Zählernummer,Zeitstempel,Zählerstand&#x0A;</xsl:text>
    <xsl:for-each select="//TP">
        <xsl:for-each select="VALUES">
            <xsl:value-of select="../@ID"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="DATETIME"/>,<xsl:value-of select="EE"/>
            <xsl:text>&#x0A;</xsl:text>
        </xsl:for-each>
    </xsl:for-each>

</xsl:template>
</xsl:stylesheet>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top