1

Can anyone please guilde me with this. I am trying to locate a particular tag, based on it child's contents and remove the parent tag and contents and add a new contents, but cant find an answer. Here is my 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>

What I want to do is to search <filtername>accesslog</filtername> exists in a <filter> block, if it does i want to delete the whole <filter> block which the <filtername>accesslog</filtername> in its parent and add a new content. So the result would be:

<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>

I'm just trying my first xsl to delete content first. Here it is:

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>

I get errors.

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"
^
2

You should not try to parse XML with a shell script. Look for a purpose built tool like xsltproc.

  • I tried writing xsl. I included that in code. I'm getting errors. Can you help me with that? – Thiripu Nov 5 '15 at 10:41
  • As the error indicates the XSLT is malformed, it seems you have two strings following doctype-public. – Bram Nov 5 '15 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.