有人能帮我做这个吗。我正在尝试定位一个特定的标签,基于它的子标签的内容,删除父标签和内容,并添加新的内容,但找不到答案。这是我的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>
块中,如果存在,我想删除父块中<filtername>accesslog</filtername>
的整个<filter>
块,并添加新内容。所以结果是:
<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
的XML输入元素名称,模板应该使用<xsl:template match="filter[filtername = 'accesslog']"/>
。
如果你想用新的内容替换这个元素,那么定义一个参数并复制它,就像在中一样
<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.