• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1147
  • Last Modified:

Changing DOCTYPE using DOM - MSXML parser 3.0

Environment
-----------

Windows NT/2000 server/workstation, Visual Basic 6.0, MSXML parser 3.0

Need
----

I need to change the DTD reference on an XML file. I need to do this programmatically using MSXML 3.0.

I tried making use of the doctype property of DOMDocument. But it's read only and I can't change and write the new DTD.

Since I am adding more data to an XML file, I also need to change the DOCTYPE reference to a new DTD file, which I am not able to do.

Any help is appreciated.

Thanks!
Guru
0
guruprasad031298
Asked:
guruprasad031298
  • 4
  • 4
  • 2
1 Solution
 
Dave_GreeneCommented:
The only way to change the <!DOCTYPE>  value in the DOM is to parse out the string and replace the value.

ODOM.Load "File.xml"

StrXML = ODOM.xml

StrXML = Replace(strXML, "DTD STRING TO REPLACE", "NEW STRING")

Set ODOM = Nothing

ODOM.LoadXML = StrXML

0
 
Yury_DelendikCommented:
Or using XSL:
  var source = new ActiveXObject("Msxml2.DOMDocument");
  source.async = false
  source.load("source.xml");

  var stylesheet = new ActiveXObject("Msxml2.DOMDocument");
  stylesheet.async = false
  stylesheet.load("change_dtd.xsl");

  var result = new ActiveXObject("Msxml2.DOMDocument");
  result.async = false
  result.validateOnParse = false;

  source.transformNodeToObject(stylesheet, result);

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

<xsl:output method="xml" indent="yes" doctype-system="another.dtd" />

<xsl:template match = "/"><xsl:copy-of select="." /></xsl:template>

</xsl:stylesheet>



0
 
guruprasad031298Author Commented:
Yury,

Your answer makes logical sense. But when I run my application, I get an error,

"The stylesheet doesn't contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.". The run time error is '-2147467259 (80004005)'.

Do you have any idea why this is happening.

Thanks!
Guru
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Yury_DelendikCommented:
Try to print stylesheet.parseError.reason. Maybe path to file is wrong.

but I think... XSL only:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0" >

<xsl:output method="xml" indent="yes" doctype-system="another.dtd" />

<xsl:template match = "/"><xsl:copy-of select="." /></xsl:template>

</xsl:stylesheet>

another part of javascript for translation :-)
0
 
guruprasad031298Author Commented:
I don't think it's a problem with your code. I ran the transformnodetoobject sample available on the MSDN site
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmmthtransformnodetoobject.asp and I get the same error.

Could it be something to do with the MSXML 3.0 parser I am using.
0
 
Yury_DelendikCommented:
Hmmm. It's okay for me.
I wrote it in "change_dtd.js" file.
0
 
guruprasad031298Author Commented:
Yury,

As you mentioned the path to the file was wrong. But now I have a crazy problem.

If I use the transformNodeToObject method, the resultant DOMDocument doesn't have any contents, like in your code

xmlDoc.transformNodeToObject stylesheet, result

where result is a DOMDocument.

Instead if I use the transformNode method and capture the output, the string has the contents which I could save to a file. But as per MSDN documentation, MSXML parser saves the encoding to UTF-16, but not the default as specified in the XML as well as the XSL file.

So, I can't open the resultant XML file using MSXML.

Do you have any pointers why the 'transformNodeToObject' method doesn't get any results. I don't get any errors though.

Thanks.
0
 
Yury_DelendikCommented:
It may be validation problem. Wrong DTD path (source document DTD path and destination document one) or invalid XML structure.

Encoding:
<xsl:output method="xml" indent="yes" doctype-system="another.dtd" encoding="UTF-8" />

0
 
Dave_GreeneCommented:
Parse that puppy...  you would be done by now!
0
 
guruprasad031298Author Commented:
I will accept this answer. I am still trying to find a reason why transferNodeToObject doesn't work for me.

Thanks!
Guru
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now