2011-04-01 59 views
0

我試圖轉換一些XML,以便iso8879實體字符串將出現在字符位置。例如,字符串1234-5678將變成1234‐5678。我已經使用字符映射和http://www.w3.org/2003/entities/iso8879doc/overview.html中的樣式表完成了這個工作。使用Saxon和XSLT轉換JDOM XML文檔

我的XSLT的第一部分看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:import href="iso8879map.xsl"/> 
    <xsl:output omit-xml-declaration = "yes" use-character-maps="iso8879"/> 

當我運行這個樣式表在Eclipse與撒克遜XSLT引擎正常工作和到位的輸出與連字符entitiy字符串的XML文件連字符。但是,我需要使這個過程自動化,所以我使用JDOM包。不幸的是,在轉換過程中角色並未被替換。執行轉換的代碼看起來有點像這樣:

System.setProperty("javax.xml.transform.TransformerFactory", 
    "net.sf.saxon.TransformerFactoryImpl"); // use saxon for xslt 2.0 support 


SAXBuilder builder = new SAXBuilder(); 
builder.setExpandEntities(false);  
XSLTransformer transformer = new XSLTransformer(styleSheet); 

Document toTransform = builder.build(Fileref); // transform 
Document transformed = transformer.transform(toTransform); 

然後我用下面的方法寫文件到一個文件:

public static void writeXMLDoc(File xmlDoc, Document jdomDoc){ 

    try { 
     Format format = Format.getPrettyFormat(); 
     format.setOmitDeclaration(true); 
     format.setEncoding("ISO-8859-1"); 
     XMLOutputter outputter = new XMLOutputter(format); 
     //outputter.output((org.jdom.Document) allChapters, System.out); 
     FileWriter writer = new FileWriter(xmlDoc.getAbsolutePath()); 
     outputter.output((org.jdom.Document) jdomDoc, writer); 
     writer.close(); 
    } 
    catch (java.io.IOException exp) { 
     exp.printStackTrace(); 
    } 
} 

我已經在Eclipse中開始調試,它看起來像連字符在xslt轉換過程中沒有被替換。我已經用它自己的Saxon xslt引擎測試過了,它確實有效,所以它可能與從Java和Jdom使用它有關。任何人都可以幫忙嗎?

非常感謝。

吉姆

+0

字符映射未完成的變換,它的輸出序列的一部分,所以你可能看不到他們在變換中改變。序列化輸出的是JDOM,而不是撒克遜。 – 2011-04-01 19:19:54

+0

「我需要自動化這個過程,所以我使用JDOM包」......後者不是由前者規定的結論。撒克遜工作,所以爲什麼不使用撒克遜自動化過程? – 2011-04-01 20:53:43

+0

事實上,如果您有充分的理由使用JDOM,您可以將其與Saxon一起使用。當您使用其XSLTransformer API時,JDOM使用XSLT 1.0 Xalan處理器,但Saxon也將接受JDOM文檔作爲輸入,允許您使用XSLT 2.0字符映射。 – 2011-04-02 22:14:30

回答

2

問題確實練得與不使用由撒克遜提供的JDOM包裝類。下面是引用,顯示正在轉化並恢復爲一個新JDOM文檔JDOM文檔的工作代碼:

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); // use saxon for xslt 2.0 support 
File styleSheet = new File("filePath"); 

// Get a TransformerFactory 
System.setProperty("javax.xml.transform.TransformerFactory", 
        "com.saxonica.config.ProfessionalTransformerFactory"); 
TransformerFactory tfactory = TransformerFactory.newInstance(); 
ProfessionalConfiguration config = (ProfessionalConfiguration)((TransformerFactoryImpl)tfactory).getConfiguration(); 

// Get a SAXBuilder 
SAXBuilder builder = new SAXBuilder(); 

//Build JDOM Document 
Document toTransform = builder.build(inputFileHandle); 

//Give it a Saxon wrapper 
DocumentWrapper docw = new DocumentWrapper(toTransform, inputHandle.getAbsolutePath(), config); 

// Compile the stylesheet 
Templates templates = tfactory.newTemplates(new StreamSource(styleSheet)); 
Transformer transformer = templates.newTransformer(); 

// Now do a transformation 
ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);     
transformer.transform(docw, new StreamResult(outStream)); 

ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); 
Document transformed = builder.build(inStream); 
+0

這是來自Michael Kay的文章:「這是Saxon 9.0的文檔,你真的想下載Saxon 9.0嗎?如果你有一個很好的理由,但它的方式已經過時 - 例如,JDOM支持適用於舊版本對於Saxon-B,看看SourceForge,對於Saxon-SA,它將在Saxonica網站上,儘管不再積極上市 在最近的版本中,JDOM支持捆綁在Saxon-PE和更高版本中;對於Saxon-HE if你真的希望JDOM支持你可以下載源代碼並編譯它。「 因此,請注意,這是撒克遜舊版本的代碼。 – 2013-09-11 12:53:50

+0

如果您希望使用最新的方式來執行此操作,請下載其他資源文件,該文件有一個JDOM示例:http://www.saxonica.com/download/download_page.xml – 2013-09-11 13:19:04

+1

上述代碼仍然可以正常工作,但Saxon 9.5從9.0開始,您安裝必要的支持庫的方式發生了變化。從9.2開始,JDOM DocumentWrapper已經包含在帶有Saxon-EE和Saxon-PE的二進制JAR文件中,但只能作爲源代碼與Saxon-HE(源代碼位於saxon-resources下載中)一起提供。 – 2013-09-11 14:51:25