Using XML and XSL together

Discussion in 'XML' started by Caroline M, Jan 18, 2007.

  1. Caroline M

    Caroline M Guest

    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, Jan 18, 2007
    #1
    1. Advertising

  2. Caroline M

    Caroline M Guest

    > <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.
     
    Caroline M, Jan 18, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,688
    A. Bolmarcich
    May 27, 2005
  2. Replies:
    2
    Views:
    9,354
    Martin Honnen
    Sep 5, 2006
  3. @do

    XML to XML using XSL

    @do, Sep 27, 2006, in forum: XML
    Replies:
    3
    Views:
    418
  4. shapper
    Replies:
    2
    Views:
    334
    shapper
    Nov 19, 2006
  5. generalr
    Replies:
    0
    Views:
    1,196
    generalr
    Apr 20, 2010
Loading...

Share This Page