0

I am trying to run a Java code, that is related to converting xml to csv. The use of an xsl file is necessary. However, some problems seem to exist in the code, because when running the Java code I am receiving an empty csv file (with only title of each column present). The Java code works really fine, because I used it with some test data.

So the problem is reggarding the xsl file.

My xml file looks like:

 <?xml version="1.0" encoding="UTF-8"?>
    <fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
        <data time="0.00">
            <mobil id="1" x="23.774532" y="37.967331" angle="229.707852" type="car" speed="0.000000" pos="5.100000" lane="32041497_0" slope="0.000000"/>
            <mobil id="2" x="23.758638" y="37.971738" angle="38.291786" type="car" speed="0.000000" pos="5.100000" lane="265887574#0_0" slope="0.000000"/>
        </data>
        <data time="1.00">
            <mobil id="1" x="23.774522" y="37.967326" angle="230.554332" type="car" speed="1.000000" pos="6.100000" lane="32041497_0" slope="0.000000"/>
            <mobil id="2" x="23.758645" y="37.971745" angle="38.291786" type="car" speed="1.000000" pos="6.100000" lane="265887574#0_0" slope="0.000000"/>
        </data>
        <data time="2.00">
            <mobil id="1" x="23.774503" y="37.967316" angle="233.076683" type="car" speed="2.000000" pos="8.100000" lane="32041497_0" slope="0.000000"/>
            <mobil id="2" x="23.758660" y="37.971759" angle="38.291786" type="car" speed="2.000000" pos="8.100000" lane="265887574#0_0" slope="0.000000"/>
        </data>

While my xsl file:

  <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="/">
    x
    <xsl:for-each select="/data">
    <xsl:value-of select="/data/mobil/@x" />
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

What I want is to declare in lines 5-7 that I want the values of the attribute: x of mobil etiquette.

2
  • 1. Your XML file seems cut off: There is no closing tag for fcd-export. 2. If fcd-export is the root element, then your XSLT will not do anything, because it assumes data is the root element. Commented Oct 21, 2019 at 14:52
  • well, actually, i mean in the real file there is </fcd-export> in the end. Commented Oct 21, 2019 at 15:43

1 Answer 1

1

Assuming that your input actually looks like:

XML

<?xml version="1.0" encoding="UTF-8"?>
<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
    <data time="0.00">
        <mobil id="1" x="23.774532" y="37.967331" angle="229.707852" type="car" speed="0.000000" pos="5.100000" lane="32041497_0" slope="0.000000"/>
        <mobil id="2" x="23.758638" y="37.971738" angle="38.291786" type="car" speed="0.000000" pos="5.100000" lane="265887574#0_0" slope="0.000000"/>
    </data>
    <data time="1.00">
        <mobil id="1" x="23.774522" y="37.967326" angle="230.554332" type="car" speed="1.000000" pos="6.100000" lane="32041497_0" slope="0.000000"/>
        <mobil id="2" x="23.758645" y="37.971745" angle="38.291786" type="car" speed="1.000000" pos="6.100000" lane="265887574#0_0" slope="0.000000"/>
    </data>
    <data time="2.00">
        <mobil id="1" x="23.774503" y="37.967316" angle="233.076683" type="car" speed="2.000000" pos="8.100000" lane="32041497_0" slope="0.000000"/>
        <mobil id="2" x="23.758660" y="37.971759" angle="38.291786" type="car" speed="2.000000" pos="8.100000" lane="265887574#0_0" slope="0.000000"/>
    </data>
</fcd-export>

you can use the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/fcd-export">
    <xsl:for-each select="data/mobil">
        <xsl:value-of select="@x"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

to get:

Result:

23.774532
23.758638
23.774522
23.758645
23.774503
23.758660

Note the use of <xsl:for-each select="data/mobil"> instead <xsl:for-each select="/data">. The latter tries to select a root element named data which does not exist. And also <xsl:value-of select="@x"/> instead of <xsl:value-of select="/data/mobil/@x" />. Your version (if it worked) would only get the value from the first mobil.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.