根据子标记删除标记和内容,并使用xsltproc在xml中添加新内容

时间:2015-11-05 10:49:39

标签: xml xslt xslt-1.0

任何人都可以请这个骂我。 我试图找到一个特定的标签,根据它的孩子的内容,删除父标签和内容,并添加一个新的内容,但无法找到答案。这是我的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"
^

1 个答案:

答案 0 :(得分: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"/>

如果XML输入元素名称为filtername,则模板应使用<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在线。