MSXML - Namespaces info not persisting - delphi

I am using MSXML 6.0 to perform a transform of my own XML into another XML format. I am not sure if I maybe just don't understand exactly how MSXML works, but I believe I have noticed some strange behaviour with it....
I am adding in the namespaces to my XML doc using the setProperty method e.g.
XmlDocument.setProperty('SelectionNamespaces',
' xmlns:ms=''http://mydomain.com/2010/MySchema''');
Then I am building up the XML using my own custom serializer in memory (not saving to disk). Once serialized I then load in the XSLT file and perform the transformation using transformNodeToObject e.g.
AppXmlDoc.transformNodeToObject(XslXmlDoc, AStreamForTransformedXml);
The problem is the transform is working but none of the specific template matching XPath I have in it is. I eliminated any problems with the XSLT file itself by running it with test data through Visual Studio and it worked as expected. I then assumed it must have been an encoding issue so I made sure that all the documents involved were being read/written out as UTF-8....still no luck.
Here is an example of what the transform looks like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://mydomain.com/2010/MySchema" exclude-result-prefixes="ms">
<xsl:template match="/">
<ARoot>
<head>
<xsl:apply-templates select="ms:Element/ms:SubElement" />
</head>
<body>
<xsl:apply-templates select="ms:Element/ms:DifferentSubElement" />
</body>
</ARoot>
So the result of the transform when run through MSXML brings over the basic structure but does not include any of the template data. After some testing I discovered the only way to get it to work is to do it in the following steps:
Create a new XML doc
Set the namespace info using setProperty
Serialize the XML and save to disk.
Close the document - extra step
Create a new Xml doc - extra step
Reload the document - extra step
Re-set the namespace info - extra step
Perform the transform.
So it appears that MSXML loses track of the Namespace information at somepoint. What makes it even more weird is even if you reset the namespace info (after serializing) and try the transform it still doesn't work! It will only seem to work if I save the document, close it and recreate a new XML document and load it back in (which as a consequence I need to reset the namespaces).
Anyone have any thoughts/ideas on this?

The SelectionNamespace property is used for XPath with the selectNodes and selectSingleNode methods, not for XSLT or other purposes. So I am not sure why you set it and what you expect it to help with as you do not seem to use selectNodes or selectSingleNode.
What is it that you want to achieve with MSXML 6? If you want to create some MSXML DOM documents with elements and/or attributes in namespaces then make sure you use createNode as that is the only namespace aware method in MSXML's API.

Related

XSLT functions with an object as params

I've been going through some of our XSLT code and I'm starting to notice quite a bit of duplicate code. As I do like code DRY and am an OO guy, I would like to make some simple functions and call them for any of this duplicate logic. My issue is that in some cases I'd be passing in 10 parameters or some such where as another call would only be passing in 4. I know you can default parameters, but having 10 parameters to a function is just ugly. In a normal OO programming language, you'd create an object with 10 properties and pass that in which allows for the properties to grow without having to change any callers or the signature of the question in general. I know we have complex types in xsl. So should I create a separate xsd for these functions and then pass it in like:
<xsl:function name="foo:doSomething">
<xsl:param name="someComplexType"/>
...
<xsl:variable name="field1" select="someComplexType/field1" />
</xsl:function>
But then how should I build my new type with some 10 different selectors? Is this even the best way to do this? Or should I just pass in all 10 params? Conceptually the 10 different params are related to one object so it makes sense and seems cleaner to do it this way, but I'm still looking for more of a best practice for xslt going forward.
If you want to define a complex type with a schema and use that in XSLT then you need a schema-aware XSLT 2.0 or 3.0 processor like Saxon 9 Enterprise Edition.
You can however pass in an element with sub elements, without using schemas, as with XSLT 2.0 you can easily construct temporary XML nodes in your XSLT code, as in
<xsl:variable name="foo1" as="element(foo)">
<foo>
<bar><xsl:value-of select="whatever"/></bar>
<baz><xsl:value-of select="whatelse"/></baz>
...
</foo>
</xsl:variable>

Transform Multiple xml input source as stream to one xslt using saxon

I am new to saxon and xslt, we have business in which feeder delivere more than one xml files, xslt generated from altov) create one output xml files , we have selected saxon as transformer.
so far i am able to transform single xml file
do any body have example where xslt takes more than one xmls as input stream, transform using saxon.
Thanks & regards,
Kumar
You haven't told us enough about the requirements, but there are several techniques to be aware of:
You can pass additional documents as stylesheet parameters declared using xsl:param
You can read a document (given its URI) using the doc() or document() functions
You can read a whole collection of documents (e.g. the contents of a directory) using the collection() function

What are the alternatives to store a external XML file without the XSLT document function in XSLT2.0?

What are the alternatives to store a external XML file without the XSLT document function in XSLT2.0
The document functions exists since XSLT 1.0, but not to store a secondary XML document, but rather to load it.
With XPath and XSLT 2.0 for a single document you can also use the doc function to load the document e.g. <xsl:apply-templates select="doc('foo.xml')//bar"/>. And there is the collection function which often allows accessing a collection of files (e.g. those in a directory) but the exact syntax depends on the XSLT processor/XSLT implementation.

Combine JAXB and JAXWS for an imported XML Schema

How can I specify a JAXB Binding for an imported XSD within a WSDL when using wsimport?
I tried following binding, which causes the error "XPath evaluation of //xs:element[#name='isFoobar'] results in an empty target node".
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="example.wsdl"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:bindings node="wsdl:definitions">
<jaxws:bindings node="wsdl:types" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxws:bindings
node="//xs:schema[#targetNamespace='http://www.example.org/']">
<jaxb:globalBindings>
<xjc:serializable uid="10000001" />
</jaxb:globalBindings>
<jaxb:bindings
node="//xs:element[#name='isFoobar']">
<jaxb:typesafeEnumClass name="IsFoobar">
<jaxb:typesafeEnumMember value="01" name="TRUE" />
<jaxb:typesafeEnumMember value="02" name="FALSE" />
</jaxb:typesafeEnumClass>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
</jaxws:bindings>
</jaxws:bindings>
Any ideas?
Sorry for the necro-threading, I encountered this problem and although this is one of the first answer that showed up on google with various key word combination it didn't hold the answer I ended up using.
For imported schemas, the easiest way to specify a JAXB binding on an imported XSD within a WSDL is... to treat it as a completely different schema !
Short example :
MyXSD.xsd
<xsd:schema targetNamespace="whatever"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ThingThatNeedsToBeBound">
<!-- Whatever this is made of -->
</xs:complexType>
</xsd:schema>
No matter where this xsd is imported (wether it's at the root of the of my wsdl or within a nested import), all I need to write to bind my "ThingThatNeedsToBeBound" in my custom binding is :
customBindings.xml
<jaxb:bindings schemaLocation="Path/To/MyXSD.xsd" node="/xs:schema/xs:complexType[#name='ThingThatNeedsToBeBound']">
<!-- your custom binding -->
</jaxb:bindings>
So, it's just like a regular case, except that you specify the schemaLocation, but then you can consider the imported schema as a whole itself instead of a part of something.
I hope this will help others stumbling upon this problem.
Source : http://www.oracle.com/technetwork/articles/grid/jax-ws-jaxb-customization-082750.html
(Note : in the source, the solution seems way more complicated, so my case might have been simpler than what they described, I found my solution using that none the less !)
I did something similar ages ago, I think you need to specify the node to select with XPath as follows:
//xs:element[#name='isFoobar']/xs:complexType
Or replace xs:complexType with whatever kind of type you are using here. Hopefully it will fix your probelm.
My first try at resolving this was trying to somehow use XPath or multiple jxb:binding elements, but that didn't work. As far as I know the XPath just isn't validated properly against imported schemas unless it would all be rewritten and mashed together with DOM.
So the way I resolved this problem was by using inline customizations in the imported XSD. I didn't test this approach with multiple nested imports, but if you got access and time to modify all the imported XSDs this should work out ok. In my opinion this is only necessary if you need to generate the mapping and can be scrubbed/removed from the XSD once the mapping is done.

xmltask confused about dtd

I'm trying to use xmltask for ant to modify a file in a subdirectory:
project/path/to/file.xml
The file refers to a DTD like this:
<!DOCTYPE data SYSTEM "mydtd.dtd">
I don't have the flexibility to change these documents.
This DTD is stored in the same subdirectory, which has always worked fine:
project/path/to/mydtd.dtd
Unfortunately, xmltask is trying to locate the dtd in my project's top-level directory, which is where my build file is located, and where I run from:
[xmltask] java.io.FileNotFoundException: /home/me/project/mydtd.dtd (The system cannot find the file specified)
I see in the xmltask documentation that I can correct this with an xmlcatalog element to tell it where to look up the file. But I need to use a dtd element, and I can only find examples for this element, not documentation; the examples show only a publicId, and if I understand XML correctly this document does not have one. I shouldn't need to specify this, anyway, right, since my document already says my DTD is stored locally and shows right where it is?
Why isn't xmltask finding the DTD correctly? What's the best way to correct or work around this situation?
An XML Catalog is the way to go here, it just needs a bit more perseverance.
As you correctly pointed out, the standard Ant <XmlCatalog> type only allows you to specify public DTD references when using the inline syntax, which is of no use to you. However, <XmlCatalog> also lets you specify a standard OASIS-syntax catalog, which is far richer, including resolving SYSTEM DTD references.
An OASIS catalog (full spec here) looks like this:
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<system systemId="mydtd.dtd" uri="project/path/to/mydtd.dtd"/>
</catalog>
You can then reference this catalog from the <XmlCatalog>:
<xmlcatalog refid="commonDTDs"/>
<catalogpath>
<pathelement location="path/to/oasis.catalog"/>
</catalogpath>
</xmlcatalog>
And that's that. It's a good idea to build up a reusable OASIS catalog file, and refer to it from various XML-related Ant tasks, all of which can use <XmlCatalog>.
As an alternative, it looks like I can skip the whole validation by creating a blank file with the same name as the DVD file, and then deleting the file when I am done. Odds are I am going to go that route instead of using the catalog.
xmltask isn't finding it because it is looking in the current working directory. Ant allows you to specify a base directory using the basedir attribute of the <target> element. So I suggest you try this:
<target basedir="path/to" ...>
<xmltask...
</target>
It strikes me that it is not the XML/DTD that you really have the problem with, but getting xmltask to interact with the two of them as you want.
If that fails, you could use the Ant Copy task to copy the XML and DTD to the root folder before processing with xmltask, then copying back again.
Have you tried:
<!DOCTYPE data SYSTEM "./path/to/mydtd.dtd">
? Or an absolute path?
Also, you can find <dtd> description here.
I had a similar problem where an XML file had a doctype with SYSTEM reference that could not be changed.
<!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd">
I first went down the road and created a catalog file with the OASIS catalog as described above, but to be able to use external catalogs I had to include the Apache Commons Resolver 1.1 (resolver.jar) in the Ant classpath (see http://ant.apache.org/manual/Types/xmlcatalog.html).
Because I had multiple machines on which this build was supposed to run this seemed overkill, especially since xmltask worked fine if I just removed the doctype definition. I wasn't allowed to remove it permanently because the doctype was needed elsewhere.
Ultimately I used this workaround: I commented out the doctype definition using Ant's replace task, ran the xmltask, and then put the doctype back into the file.
<replace file="myxmlfile.xml">
<replacetoken><!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd"></replacetoken>
<replacevalue><!-- !DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd" --></replacevalue>
</replace>
<xmltask .../>
<replace file="${local.opencms.webapp.webinf}/config/opencms-modules.xml">
<replacetoken><!-- !DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd" --></replacetoken>
<replacevalue><!DOCTYPE opencms SYSTEM "http://www.opencms.org/dtd/6.0/opencms-modules.dtd"></replacevalue>
</replace>

Resources