Delphi/MSXML: XPath queries fail - delphi

I've loaded a XML document, and now I wish to run a XPath query to select a certain subset of the XML. The XML is
<?xml version="1.0"?>
<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with
XML.</description>
</book>
</catalog>
and the procedure goes something like
procedure RunXPathQuery(XML: IXMLDOMDocument2; Query: string);
begin
XML.setProperty('SelectionLanguage', 'XPath');
NodeListResult := XML.documentElement.selectNodes(Query));
ShowMessage('Found (' + IntToStr(NodeListResult.length) + ') nodes.');
end;
Problem is: when I run the XPath query '/catalog' for the above XML, it returns (as expected) a nodelist of 1 element. However, if I remove :xsi from
<catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> and re-run the query, the nodelist returned is empty. If I remove the entire 'xmlns'-attribute, the resulting nodelist has, once again, 1 element.
So my question is this: what can I do to remedy this, i.e. how do I make MSXML return the correct number of instances (when running a XPath query), regardless of the namespace (or other attributes)?
Thanks!

See this link!
When you use <catalog xmlns='http://www.w3.org/2001/XMLSchema-instance'> then the whole node will be moved to a different (default) namespace. Your XPath isn't looking inside this other namespace so it can't find any data. With <catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> you're just declaring xsi as a different namespace. This would be a different namespace than the default namespace.
I can't test it right now but adding something like this might help:
XML.setProperty('SelectionNamespaces', 'xmlns=''http://www.w3.org/2001/XMLSchema-instance''');
Or maybe it doesn't. As I said, I can't test it right now.

Figured it out. It seems that my problem has been described here and here (and most likely a zillion other places, too).
The query /*[local-name()='catalog'] works for me.

Use:
document.setProperty('SelectionNamespaces', 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"')

/*[local-name()='catalog']
is a solution to your question.
But why would you want to ignore namespaces? They have been introduced to express something, e.g. to distinguish different types of catalogs. With your query, you can now select the content of any catalog in the world, but I assume you can only handle books. What will happen if you get a catalog of screws or cars instead?
The mentioned things about the prefix (xsi) is correct. If you remove the prefix, all elements are in that namespace (called default namespace then). But you can still deal with it.
In your code, give the namespace a prefix anyway. It needn't even match the original prefix:
XML.setProperty('SelectionNamespaces', "xmlns:xyz='http://www.w3.org/2001/XMLSchema-instance'");
The second thing is to adapt the XPath query. It must then be
/xyz:catalog
The original XML only declares the xsi namespace, but never makes use of it. In this case, you can remove it completely. If you want to use the namespace and you want it with prefixes, then rewrite your XML to
<?xml version="1.0"?>
<xsi:catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<xsi:book id="bk101">
<xsi:author>Gambardella, Matthew</xsi:author>
<xsi:title>XML Developer's Guide</xsi:title>
<xsi:genre>Computer</xsi:genre>
<xsi:price>44.95</xsi:price>
<xsi:publish_date>2000-10-01</xsi:publish_date>
<xsi:description>An in-depth look at creating applications with
XML.</xsi:description>
</xsi:book>
</xsi:catalog>

Related

Adding line break in NodeAttributes in OmniXML

I have an XML where I have several attributes for one node:
var
row : IXMLNode;
rowattr : IXMLAttr;
xml : IXMLDocument;
begin
xml := ConstructXMLDocument('xml');
SetNodeAttr(xml.DocumentElement, 'version', '1.0');
SetNodeAttr(xml.DocumentElement, 'encoding', 'UTF-8');
row := AppendNode(xml, 'Links');
rowattr:=xml.CreateAttribute('Link1');
rowattr.Value:='http:\\wwww.somelink1.com';
row.Attributes.SetNamedItem(rowattr);
rowattr:=xml.CreateAttribute('Link2');
rowattr.Value:='http:\\wwww.somelink2.com';
row.Attributes.SetNamedItem(rowattr);
rowattr:=xml.CreateAttribute('Link3');
rowattr.Value:='http:\\wwww.somelink3.com';
row.Attributes.SetNamedItem(rowattr);
XMLSaveToFile(xml, 'C:\Test1.xml', ofIndent);
end;
I wish to have every link on a separate line like this:
<xml version="1.0" encoding="UTF-8">
<Links
link1="http://www.somelink1.com"
link2="http://www.somelink2.com"
link3="http://www.somelink3.com"
/>
</xml>
OmniXML does not offer such fine grained control of output formatting. You could perhaps look to find an external XML pretty printer that will do what you need. Or you could even write your own XML library.
Before you go any further though I would like to make the point that XML was never intended to be read by humans. Its design makes no effort to be readable, and if you continue trying to make your XML as readable as possible, then you will be swimming against the tide. If you want a human readable structured file format then you might look instead at YAML which was designed with that in mind.
Another avenue to consider is the structure of the XML. Using node attributes to specify an array of values is a poor decision. Attributes are intended to be used with name/value mapping pairs. If you want to specify an array of values then you might do so like this:
<links>
<item>http://www.somelink1.com</item>
<item>http://www.somelink2.com</item>
<item>http://www.somelink3.com</item>
</links>
This is clearer than your XML and much easier to parse. Try writing code to parse your attributes and you will see what I mean.
Now, just to illustrate my point above, in YAML this would be:
Links:
- http://www.somelink1.com
- http://www.somelink2.com
- http://www.somelink3.com
Of course, all this is moot if somebody other than you is defining the format of the XML.

Orbeon formatting.xpl to show xmlns declarations?

Code
<xforms:output mediatype="text/html" value="xxforms:serialize(xxforms:call-xpl('oxf:/ops/utils/formatting/format.xpl', 'data', instance('message-instance'), 'data')/*, 'xml')"/>
Input
<REPC_IN000023NL xmlns="urn:hl7-org:v3">
....
<hl7:patientID xmlns:hl7="urn:hl7-org:v3">
<hl7:value root="2.16.840.1.113883.2.4.6.3" extension="999999035"/>
</hl7:patientID>
....
</REPC_IN000023NL>
Expected a rendering that includes the namespace declaration, but instead the rendering omits exactly that.
Bug or feature?
Indeed, the namespace declaration for the hl7 prefix, which isn't on the root element, is not produced:
I'd recommend you don't use that format.xpl; it isn't maintained, has performance issues with large documents, and, as you noticed, doesn't handle some corner cases well (i.e. it's buggy!).
Instead, you can use the fr:code-mirror component, which delegates formatting to CodeMirror. It won't do the indenting for you, but Saxon can do it for you. This example will give you the following output:

JAXB bindings for nested element with same name

I have a schema that is given to me (so I am not in position to make changes to it), and I am trying to generate the JAXB objects for it, and I get a naming conflict because of nested elements with same name.
I created bindings.xml to avoid the naming conflict, and it is working fine when I have a two level nesting (e.g. the XPath is something like this:
/A/Value/B/Value
So something like this solves the problem:
<jaxb:bindings
node="/xsd:schema/xsd:complexType[#name='A']
/xsd:complexType/xsd:sequence/xsd:element[#name='Value']
/xsd:complexType/xsd:sequence/xsd:element[#name='B']
/xsd:complexType/xsd:sequence/xsd:element[#name='Value']">
<jaxb:class name="InnerValue" />
</jaxb:bindings>
But now I have a three level nesting like this:
/A/Value/B/Value/C/Value
and I don't know how to define the bindings (whether I need two of them ) to avoid compilation errors.
Any help much appreciated
I think you have to customize your complex types, not elements. So attach your bindings to the corresponding xs:complexTypes. And yes, you will probably need two of them for inner B and C classes. I'd name them A_B and A_B_C, something like that.
See also jaxb:globalBindings/#localScoping='topLevel', might be useful.

Overriding JSF's default I18N handling

Like the asker of the question here
Variable substitution JSF Resource Bundle property file
I'm slightly aghast at the inability to reference the value of other property keys in the message bundle.
Although I see how easy to write my own rubbish handler[0] that can do what I want in a custom component, that would leave expressions in templates calling the message bundle still using the default JSF implementation.
Is it possible to override the default JSF handling of the message bundle?
[0] Or better, to use code referenced in one of the answers to the above question
https://code.google.com/p/reflectiveresourcebundle/
You can provide the fully qualified name of a concrete ResourceBundle implementation as "base name" instead of alone the path and filename of the properties files.
E.g.
public class YourCustomResourceBundle extends ResourceBundle {
// ...
}
which can be registered as follows
<application>
<resource-bundle>
<base-name>com.example.YourCustomResourceBundle</base-name>
<var>text</var>
</resource-bundle>
</application>
or declared on a per-view/template basis as follows
<f:loadBundle baseName="com.example.YourCustomResourceBundle" var="text" />
Here are several related questions/answers which contain some concrete code which you could use as a kickoff example:
How to remove the surrounding ??? when message is not found in bundle
internationalization in JSF with ResourceBundle entries which are loaded from database
i18n with UTF-8 encoded properties files in JSF 2.0 appliaction
Everything is possible for those who try. The question is not whether it is is possible but should you do it. And the answer to that question is: probably not.
Referencing other message in a message bundle means you want to build a compound message. So you can re-use part of the message many times just to save small fraction of the disk space or small fraction of development time.
If that is the case, I have a message for you. What you plan to do is called a concatenation and it is the second most common I18n defect. And its implications are as bad as those of hardcoded strings.
Why? Because target languages do not follow the English grammar rules. First, it is common need to re-order the sentence while translating. This might be easy to fix by using (numbered or named) placeholders. On the other hand though, the translation might differ depending on the context. That is, it might need to be translated in totally other way, or simply the word endings might need to be different depending on a grammar case, mood or gender.
My advice is, do not use such shortcuts, it will create more problems than it fixes.
Now you should know why "those stupid Romans" didn't implement it like this: it is against I18n best practices.

ws jaxb custom external mapping

I have to do custom JAXB external mapping file.
Already red tutorial about this and can not find any suitable example of JAXB customization. My scenario is that I have two WSDL files main WSDL and secondary WSDL which is included in main one. And in secondary WSDL file is one complexType definition which has a name that I want to customize. BTW name of that complexType is objectFactory. So now you know what I really really need that external customization.
I have came so far that when I run wsimport path-to-my-main-wsdl -b customBindings.jaxb
output is like
[ERROR] XPath evaluation of "//xs:complexType[#name='objectFactory']" results in empty target node
line 2 of file:/customBindings.jaxb
File customBindings.jaxb looks like
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="path-to-my-secondary-wsdl#types?schema1" node="//xs:complexType[#name='objectFactory']">
<jxb:class name="MyObjectFactory" />
</jxb:bindings>
</jxb:bindings>
At second line of my customBindings.jaxb file are two properties schemaLocation and node. Does property schemaLocation need to point at main WSDL or secondary WSDl? And does property node be exact path (XPath) to wanted complexType or is like this ok? And what is with namespaces within XPath (node property), it has to be xs or what? And what actually this types and schema1 stands for in schemaLocation?
thx
I came across your question while researching the same problem.
A few vague hints are found on this page: http://jaxb.java.net/guide/Dealing_with_errors.html
Causes for the "empty target node" message are suggested at the bottom of that page.

Resources