【问题标题】:Not able to write XML data in CSV file using XSL无法使用 XSL 在 CSV 文件中写入 XML 数据
【发布时间】:2023-03-06 15:33:01
【问题描述】:
I have a XML file like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
    <changeSet author="253503 (generated)" id="1638881200824-1">
        <insert tableName="sampletable">
            <column name="id" valueNumeric="2"/>
            <column name="name" value="k"/>
            <column name="active" valueBoolean="true"/>
            <column name="age" valueNumeric="2"/>
        </insert>
        <insert tableName="sampletable">
            <column name="id" valueNumeric="23"/>
            <column name="name" value="kathir"/>
            <column name="active" valueBoolean="true"/>
            <column name="age" valueNumeric="29"/>
        </insert>
        <insert tableName="sampletable">
            <column name="id" valueNumeric="24"/>
            <column name="name" value="gowtham"/>
            <column name="active" valueBoolean="true"/>
            <column name="age" valueNumeric="28"/>
        </insert>
        <insert tableName="sampletable">
            <column name="id" valueNumeric="25"/>
            <column name="name" value="varshan"/>
            <column name="active" valueBoolean="false"/>
            <column name="age" valueNumeric="5"/>
        </insert>
        <insert tableName="sampletable">
            <column name="id" valueNumeric="45"/>
            <column name="name" value="heal"/>
            <column name="active" valueBoolean="true"/>
            <column name="age" valueNumeric="65"/>
        </insert>
    </changeSet>
</databaseChangeLog>

And my XSL file is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:dbc="http://www.liquibase.org/xml/ns/dbchangelog"
    version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <!-- Header -->
    <xsl:apply-templates select="dbc:databaseChangeLog/dbc:changeSet/dbc:insert[1]">
      <xsl:with-param name="header">true</xsl:with-param>
    </xsl:apply-templates>
    <!-- Data -->
    <xsl:apply-templates select="dbc:databaseChangeLog/dbc:changeSet/dbc:insert"/>
  </xsl:template>
  
  <xsl:template match="dbc:insert">
    <xsl:param name="header"/>
    <xsl:for-each select="dbc:column">
      <!-- For the header take the name attribute, else take the attribute starting with value-->
      <xsl:choose>
        <xsl:when test="$header='true'">
          <xsl:value-of select="@name"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@*[starts-with(name(),'value')]"/>
        </xsl:otherwise>
      </xsl:choose>
      <!-- Insert comma between values, except for last value insert new line -->
      <xsl:choose>
        <xsl:when test="position()=last()">
          <xsl:text>&#xa;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>,</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>  
</xsl:stylesheet>

my Java code is :
 public static void main(String args[]) throws Exception { 
            File stylesheet = new File("NewStylesheet1.xsl");
            File xmlSource = new File("db.changelog9.xml");

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlSource);

            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer(stylesource);
            Source source = new DOMSource(document);
            Result outputTarget = new StreamResult(new File("/tmp/x.csv"));
            transformer.transform(source, outputTarget);
            }

我将执行直接转换(即右键单击 XML 文件 -> 作为 XSLT 转换运行并添加 XSL 文件),它会按预期生成输出,如下所示: ID,姓名,活动,年龄 2,k,真,2 23,凯瑟尔,真实,29 24,戈瑟姆,真实,28 25,varshan,假,5 45,治愈,真实,65

但是当我尝试使用 java 代码执行相同的操作时,它无法将值写入 CSV 文件中。它返回一个没有任何值的空文件。有人可以帮忙解决问题吗?

【问题讨论】:

    标签: java xml xslt


    【解决方案1】:

    在创建 DocumentBuilder 和解析 XML 输入之前,通过调用方法 https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#setNamespaceAware-boolean- 尝试让 DocumentBuilderFactory 命名空间感知:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    ...
    

    【讨论】:

    • 将java代码更新为:DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("db.changelog9.xml")); StreamSource stylesource = new StreamSource(new File("NewStylesheet1.xsl"));但还是没有运气!!相同的空 CSV 文件@Martin Honnen
    • 我没有看到您尝试调用我链接的方法,我现在已经编辑了答案以将建议拼写为代码。
    猜你喜欢
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多