2017-07-06 32 views
2

我需要驗證XML和XSD不是很接受的(默認的使用太多的xsd:序列和招用的xsd:選擇使得驗證過可以接受的(不知道這是正確的字))瞭解XML擺脫文本內容

那麼,有沒有把這個

<?xml version="1.0" encoding="UTF-8"?> 
<bookstore> 
    <book category="cooking"> 
    <title lang="en">Everyday Italian</title> 
    <author>Giada De Laurentiis</author> 
    <year>2005</year> 
    <price>30.00</price> 
    </book> 
    <book category="children"> 
    <title lang="en">Harry Potter</title> 
    <author>J K. Rowling</author> 
    <year>2005</year> 
    <price>29.99</price> 
    </book> 
    <book category="web"> 
    <title lang="en">Learning XML</title> 
    <author>Erik T. Ray</author> 
    <year>2003</year> 
    <price>39.95</price> 
    </book> 
</bookstore> 

進入這個

<?xml version="1.0" encoding="UTF-8"?> 
<bookstore> 
    <book category="cooking"> 
    <title lang="en"></title> 
    <author></author> 
    <year></year> 
    <price></price> 
    </book> 
    <book category="children"> 
    <title lang="en"></title> 
    <author></author> 
    <year></year> 
    <price></price> 
    </book> 
    <book category="web"> 
    <title lang="en"></title> 
    <author></author> 
    <year></year> 
    <price></price> 
    </book> 
</bookstore> 

在使用Python/Java的窗戶好方法/去?它不是一次性工作,我需要自動完成

+0

你可以找到一種方法來解決你的問題與正則表達式,檢查[這個主題](https://stackoverflow.com/questions/7167279/regex-select-all-text-between-tags) –

回答

1

XML轉換的正確工具是XSLT。這個很容易。在XSLT 3.0是

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform version="3.0"> 
    <xsl:output indent="yes"/> 
    <xsl:mode on-no-match="shallow-copy"/> 
    <xsl:template match="text()"/> 
</xsl:stylesheet> 

你說一個Java的解決方案是確定的,所以下載撒克遜-HE 9.8,如果你更喜歡使用XSLT 1.0或2.0的處理器可以替換運行此作爲

java net.sf.saxon.Transform -s:in.xml -xsl:trans.xsl -o:out.xml 

xsl:mode聲明與身份模板規則很容易搜索到。