2015-11-05 73 views
0

任何人都可以用這個給我guilde。 我想找到一個特定的標籤,基於它的孩子的內容,並刪除父標籤和內容,並添加一個新的內容,但無法找到答案。這裏是我的xml:刪除基於子標籤的標籤和內容,並使用xsltproc在xml中添加新內容

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE app 
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
<app> 
<displayname>Admin</displayname> 
<filter> 
<filtername>accesslog</filtername> 
<filterclass>com.filter.accesslog</filterclass> 
</filter> 
<filter> 
    <filtername>ServerHealthCheckFilter</filtername> 
    <filterclass>com.filter.ServerHealthCheckFilter</filterclass> 
</filter> 
</app> 

我想要做的是搜索<filtername>accesslog</filtername>存在於<filter>塊,如果是這樣我想刪除整個<filter>塊,在其父<filtername>accesslog</filtername>和添加新的內容。所以結果是:

<displayname>Admin</displayname> 
<filter> 
<filtername>accesslog</filtername> 
<filterclass>com.logclient.filter.accesslog</filterclass> 
<initparam> 
    <param-name>logClientName</param-name> 
    <param-value>com.logging.AccessLogImpl</param-value> 
    </initparam> 
</filter> 
<filter> 
    <filtername>ServerHealthCheckFilter</filtername> 
    <filterclass>com.filter.ServerHealthCheckFilter</filterclass> 
</filter> 

我只是想我的第一個xsl先刪除內容。那就是:

modifyxml.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output 
    method = "xml" 
    version = "1.0" 
    encoding = "ISO-8859-1" 
    omit-xml-declaration = "yes" 
    doctype-public = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" 
    indent = "yes"/> 
<xsl:output omit-xml-declaration="yes"/> 
<xsl:template match="node()|@*"> 
<xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
</xsl:copy> 
</xsl:template> 
<xsl:template match="filter[filter-name = 'accesslog']"/> 
</xsl:stylesheet> 

我得到的錯誤。

modifyxml.xsl:8: parser error : error parsing attribute name 
"http://java.sun.com/dtd/web-app_2_3.dtd" 
^ 
modifyxml.xsl:8: parser error : attributes construct error 
"http://java.sun.com/dtd/web-app_2_3.dtd" 
^ 
modifyxml.xsl:8: parser error : Couldn't find end of Start Tag output line 2 
"http://java.sun.com/dtd/web-app_2_3.dtd" 
^ 

回答

0

這是一個XML和XSLT語法錯誤,我想你想

<xsl:output 
    method = "xml" 
    version = "1.0" 
    encoding = "ISO-8859-1" 
    omit-xml-declaration = "yes" 
    doctype-public = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
doctype-system="http://java.sun.com/dtd/web-app_2_3.dtd" 
    indent = "yes"/> 

而且給出的filtername模板應該使用<xsl:template match="filter[filtername = 'accesslog']"/>一個XML輸入元素名稱。

如果要更換新的內容元素,則定義參數,並複製它,在

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:param name="new-filter"> 
<filter> 
<filtername>accesslog</filtername> 
<filterclass>com.logclient.filter.accesslog</filterclass> 
<initparam> 
    <param-name>logClientName</param-name> 
    <param-value>com.logging.AccessLogImpl</param-value> 
    </initparam> 
</filter>  
</xsl:param> 

<xsl:output 
    method = "xml" 
    version = "1.0" 
    encoding = "ISO-8859-1" 
    omit-xml-declaration = "yes" 
    doctype-public = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
doctype-system="http://java.sun.com/dtd/web-app_2_3.dtd" 
    indent = "yes"/> 


<xsl:template match="node()|@*"> 
<xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
</xsl:copy> 
</xsl:template> 

<xsl:template match="filter[filtername = 'accesslog']"> 
    <xsl:copy-of select="$new-filter"/> 
</xsl:template> 



</xsl:stylesheet> 

在線http://xsltransform.net/pPzifp9