【问题标题】:xsl code problem when running Java code related to data conversion运行与数据转换相关的Java代码时出现xsl代码问题
【发布时间】:2019-10-21 14:41:37
【问题描述】:

我正在尝试运行与将 xml 转换为 csv 相关的 Java 代码。 必须使用 xsl 文件。但是,代码中似乎存在一些问题,因为在运行 Java 代码时,我收到一个空的 csv 文件(只有每列的标题)。 Java 代码工作得很好,因为我将它与一些测试数据一起使用。

所以问题出在 xsl 文件上。

我的 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>

而我的 xsl 文件:

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

我想要的是在第 5-7 行声明我想要属性的值:x of mobil etiquette。

【问题讨论】:

  • 1. 您的 XML 文件似乎被截断:fcd-export 没有结束标记。 2. 如果fcd-export 是根元素,那么您的XSLT 将不会做任何事情,因为它假定data 是根元素。
  • 嗯,实际上,我的意思是在真实文件中最后有 。

标签: xml xslt


【解决方案1】:

假设您的输入实际上是这样的:

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>

您可以使用以下样式表:

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>

得到:

结果:

23.774532
23.758638
23.774522
23.758645
23.774503
23.758660

注意使用&lt;xsl:for-each select="data/mobil"&gt; 而不是&lt;xsl:for-each select="/data"&gt;。后者试图选择一个不存在的名为data 的根元素。还有&lt;xsl:value-of select="@x"/&gt; 而不是&lt;xsl:value-of select="/data/mobil/@x" /&gt;。您的版本(如果有效)只会从第一个 mobil 中获取值。

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多