You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by zmustansar <zm...@hotmail.com> on 2007/02/26 10:14:25 UTC

How to insert/update in XML document

I am in a process of editing a xml document. i.e update certain nodes or
addition of some in document.

I s there any easy way of doing that.

Zeeshan
-- 
View this message in context: http://www.nabble.com/How-to-insert-update-in-XML-document-tf3291671.html#a9155578
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: How to insert/update in XML document

Posted by Jimmy Zhang <jz...@ximpleware.com>.
VTD-XML is very good for that
http://vtd-xml.sf.net

----- Original Message ----- 
From: "zmustansar" <zm...@hotmail.com>
To: <xa...@xml.apache.org>
Sent: Monday, February 26, 2007 1:14 AM
Subject: How to insert/update in XML document


>
> I am in a process of editing a xml document. i.e update certain nodes or
> addition of some in document.
>
> I s there any easy way of doing that.
>
> Zeeshan
> -- 
> View this message in context: 
> http://www.nabble.com/How-to-insert-update-in-XML-document-tf3291671.html#a9155578
> Sent from the Xalan - J - Users mailing list archive at Nabble.com.
>
> 



Re: How to insert/update in XML document

Posted by Mike Brown <mi...@skew.org>.
zmustansar wrote:
> I am in a process of editing a xml document. i.e update certain nodes or
> addition of some in document.
> 
> I s there any easy way of doing that.

Well, you're asking on the Xalan-J list, so I assume you're wanting an answer
relevant to Xalan-J.

Xalan is an XSLT processor, which means it follows rules defined in a
stylesheet in order to create a new document based on an existing one. It
doesn't modify the original document.

You can use standard JAXP interfaces (javax.xml.*) to invoke Xalan-J with a
DOMSource wrapping your original Document object and (assuming you want a
DOM), a DOMResult to provide the product of the XSLT transformation as another
Document object. I trust you can read the documentation on how to do that.

A typical XSLT design pattern for making small changes to a document is to use
a stylesheet containing a template that directs the processor to perform the
"identity transformation": a recursive copy-through of the nodes in the source
document. You supplement this template with other templates to override the
handling of certain nodes. In this way, you can 'modify' the document.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="no"/>

  <!--default for any node: recursively copy it-->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!--override for 'foo' elements: rename them 'bar'-->
  <xsl:template match="foo">
    <bar>
      <xsl:apply-templates select="@*|node()"/>
    </bar>
  </xsl:template>

  <!--override for 'baz' elements: eliminate them and their contents completely-->
  <xsl:template match="baz"/>

</xsl:stylesheet>

For general questions about XSLT, use xsl-list, about which there is info
at http://www.mulberrytech.com/xsl/xsl-list/

There are other approaches to updating an XML document besides using XSLT. You
could use DOM interfaces directly, you could use an XUpdate processor (though
in either case, if you're going to go to that much trouble, you'll find XSLT
easier and more robust), or you could reparse and manipulate the document
using some other library that provides a DOM-like but easier/more efficient
programmatic API. I believe there are quite a few of these available for
various programming languages.

Mike