[英]XSL for converting XML to CSV : Adding Quotes to the end based on data field
我正在嘗試使用Java代碼將XML文件動態轉換為CSV文件。 我能夠獲取轉換為CSV的數據,但是問題是我的數據包含“”和“,”。
這是我的示例XML:
<record>
<column name="ID">537316</column>
<column name="TYPE">MANUAL</column>
<column name="SECONDID">546</column>
<column name="INFO">"THIS","IS",FOR,"TEST"</column>
<column name="KEY">345</column>
</record>
這是Java代碼:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
class xmltocsv {
public static void main(String args[]) throws Exception {
File stylesheet = new File("C:/testxsl.xsl");
File xmlSource = new File("C:/test.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("c:/output.csv"));
transformer.transform(source, outputTarget);
}
}
這是我的XSL文件:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<xsl:for-each select="*[1]/*">
<xsl:text>"</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()">,</xsl:if>
<xsl:if test="position() = last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:param name="fieldNames" select="'yes'" />
<xsl:strip-space elements="*" />
<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
<xsl:if test="position() != last()"><xsl:text>"</xsl:text><xsl:value-of Select="normalize-space(.)"/><xsl:text>"</xsl:text>,</xsl:if>
<xsl:if test="position() = last()"><xsl:text>"</xsl:text><xsl:value-of select="normalize-space(.)"/><xsl:text>"</xsl:text><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
樣本輸出應為:
ID,TYPE,SECONDID,INFO,KEY
"537316","MANUAL","546","THIS"",""IS"",FOR,""TEST""","345"
但是我得到的輸出是:
ID,TYPE,SECONDID,INFO,KEY\n
"537316","MANUAL","546",""THIS","IS",FOR,"TEST"","345"
我使用的XML來自數據庫,並且包含特殊字符(“),這會在輸出CSV中導致意外結果(當我使用MS Excel打開輸出CSV時)。我需要驗證數據是否為引號以及是否存在引號必須添加額外的引號以獲得所需的輸出。有人可以幫助我解決在XSL中可以用來驗證字符串並在數據中搜索(“”)的if條件。
class XmlToCsv {
private static void emitHeaders( Node record ){
NodeList fields = record.getChildNodes();
String del = "";
for( int iField = 0; iField < fields.getLength(); iField++ ){
Node node = fields.item( iField );
if( ! ( node instanceof Element ) ) continue;
System.out.print( del );
System.out.print( ((Element)node).getAttribute("name") );
del = ",";
}
System.out.println();
}
private static void emitData( Node record ){
NodeList fields = record.getChildNodes();
String del = "";
for( int iField = 0; iField < fields.getLength(); iField++ ){
Node node = fields.item( iField );
if( ! ( node instanceof Element ) ) continue;
System.out.print( del );
String cont = node.getTextContent();
cont = cont.replaceAll( "\"", "\"\"" );
System.out.print( '"' + cont + '"' );
del = ",";
}
System.out.println();
}
public static void main(String args[]) throws Exception {
File xmlSource = new File("test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlSource);
Source source = new DOMSource(document);
Element table = document.getDocumentElement();
NodeList records = table.getElementsByTagName("record");
emitHeaders( records.item( 0 ) );
for( int iRec = 0; iRec < records.getLength(); iRec++ ){
emitData( records.item( iRec ) );
}
}
}
使用JAXB甚至會更加簡單。
以下樣式表:
XSLT 1.0
<?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>ID,TYPE,SECONDID,INFO,KEY </xsl:text>
<xsl:for-each select="record/column">
<xsl:text>"</xsl:text>
<xsl:call-template name="substitute">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="substitute">
<xsl:param name="text"/>
<xsl:param name="searchString">"</xsl:param>
<xsl:param name="replaceString">""</xsl:param>
<xsl:choose>
<xsl:when test="contains($text,$searchString)">
<xsl:value-of select="substring-before($text,$searchString)"/>
<xsl:value-of select="$replaceString"/>
<xsl:call-template name="substitute">
<xsl:with-param name="text" select="substring-after($text,$searchString)"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
當應用於示例輸入時,將產生以下輸出:
ID,TYPE,SECONDID,INFO,KEY
"537316","MANUAL","546","""THIS"",""IS"",FOR,""TEST""","345"
我認為這是CSV格式的輸入數據的正確表示形式。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<xsl:for-each select="*[1]/*">
<xsl:text>"</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()">,</xsl:if>
<xsl:if test="position() = last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:param name="fieldNames" select="'yes'" />
<xsl:strip-space elements="*" />
<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
<xsl:if test="position() != last()">
<xsl:text>"</xsl:text>
<xsl:call-template name="substitute">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:if test="position() = last()"><xsl:text>"</xsl:text><xsl:value-of select="normalize-space(.)"/><xsl:text>"</xsl:text><xsl:text>
</xsl:text></xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="substitute">
<xsl:param name="text"/>
<xsl:param name="searchString">"</xsl:param>
<xsl:param name="replaceString">""</xsl:param>
<xsl:choose>
<xsl:when test="contains($text,$searchString)">
<xsl:value-of select="substring-before($text,$searchString)"/>
<xsl:value-of select="$replaceString"/>
<xsl:call-template name="substitute">
<xsl:with-param name="text" select="substring-after($text,$searchString)"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.