Using XML and XSL together

Caroline M

Usenet Archive
Joined
Jan 18, 2007
Messages
3
Reaction score
0
Hi there,

I am developing an XML-based messaging application that has to support several versions of the message specification. The way I am doing that is by using XSLT to transform messages from one version into another.

Here's a dummy example that I am using for my unit tests:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/mos"> <xsl:copy> <xsl:for-each select="*"> <xsl:copy-of select="."/> </xsl:for-each> <version_2.8>2.8.1</version_2.8> </xsl:copy> </xsl:template> </xsl:stylesheet>

This just adds a new element with version information after the others. This works fine. The trouble comes when I want one of these that only works on a specific type of message and I wanted to embed the name of the message into the XSL file. I read up on embedding XML inside XSL files and read that you could do it by putting the XSL into a separate namespace. So here is an example of how I am doing that:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mos="http://example.com/mos_data"> <mos:message_type>listMachInfo</mos:message_type> <xsl:template match="/mos"> <xsl:copy> <xsl:for-each select="*"> <xsl:copy-of select="."/> </xsl:for-each> <version_3.0>3.1</version_3.0> </xsl:copy> </xsl:template> </xsl:stylesheet>

To load these files we use a library that was developed internally as a wrapper around libxml. Its possible I'm using the library incorrectly but I want to eliminate bad syntax first. I can parse the above files successfully as both an ordinary XML document, and as an XSL document. But what I am trying to do is get hold of the data in the mos:message_type element. We have a function called "locate_single_node" which takes an XPath expression and a list of namespaces:

Here's the expression I use:

pxmlNode = pxmlDoc->locate_single_node("/mos:message_type", ns);

where ns is a list of namespaces in the document. However, the node is not found. Any ideas?

Many thanks,

Caroline M
 

Caroline M

Usenet Archive
Joined
Jan 18, 2007
Messages
3
Reaction score
0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mos="http://example.com/mos_data"> <mos:message_type>listMachInfo</mos:message_type> <xsl:template match="/mos"> <xsl:copy> <xsl:for-each select="*"> <xsl:copy-of select="."/> </xsl:for-each> <version_3.0>3.1</version_3.0> </xsl:copy> </xsl:template> </xsl:stylesheet>

Responding to my own post... you may have noticed the lack of indentation with the above file. This is because of the way it is generated programatically by my test code. One of the consequences is that it is difficult to see the hierarchy when looking at it and I fotgot that the mos:message_type node was inside the xsl:stylesheet node. Once I fixed that the code worked :)

Caroline M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top