删除基于子标签的标签和内容,并使用xsltproc在xml中添加新内容
问题描述:
任何人都可以用这个给我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"
^
答
这是一个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>