XSLT validation : input to output. Multiple namespaces

Hi all

I have an xml files with multiple prifixe for elements and nodes

x:xmlns :Root node with
d:xmlns : sub node
e:xmlns : sub of "Automobile element is define here"
 

My question is this how can I extract the value of Automobile element from
The source doc to destination doc via xslt.
Tutorial will be appreciated.

Thanks in Advance


ZURINETAsked:
Who is Participating?
 
abelCommented:
This can be done pretty well with XSLT. Remember that namespace prefixes are nothing more than placeholders and do not really matter. What matters are the namespaces themselves and the most important thing to look out for is that these namespace strings are exactly correct.

Consider this XML (just illustrative, the example has no real-world value):

<root xmlns="http://root">
    <c:child xmlns:c="http://child" />
    <o:otherchild xmlns:o="http://otherchild">some text here</o:otherchild>
    <belongsToRootNs />
</root>
then consider the following XSLT, which will output the following text:

inside root eleminside child eleminside other text node: some text here

in the XSLT I deliberately changed the prefixes to show that the namespace URI is important, and not the prefix. The output here is text, to make it simple, but outputting XML is just as straightforward.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:rt="http://root"
    xmlns:child="http://child"
    xmlns:other="http://otherchild">
    
    <xsl:output method="text" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="rt:root" />
    </xsl:template>
    
    <xsl:template match="rt:root">
        <xsl:text>inside root elem&#x0A;</xsl:text>
        <!-- apply all children in the "child" ns -->
        <xsl:apply-templates select="child:*" />
    </xsl:template>
    
    <!-- matching any element in the "http://child" ns -->
    <xsl:template match="child:*">
        <xsl:text>inside child elem&#x0A;</xsl:text>
        <xsl:apply-templates select="following-sibling::other:otherchild/text()" />
    </xsl:template>
    
    <!-- matching any textnode inside the namespace "http://otherchild" -->
    <xsl:template match="text()[parent::other:*]">
        <xsl:text>inside other text node: </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#x0A;</xsl:text>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

0
 
sundar62Commented:
Please check the attached link -
http://www.xml.com/pub/a/2001/04/04/trxml/index.html
0
 
ZURINETAuthor Commented:
Hi abel

Thanks for you help sofar..

Your solution seems to be in the right direction ..

Before I nail the coffin..

Since I have

A = input Doc xml
B = xslt
C = expected output doc , xml

How can I test the solution to see if what I programmed/filtered from
the xslt will be outputted in the out doc.

i.e run the input xml against the xslt and get my results..

That is  I need a way to test the solution..
How can I test xslt?

Regards
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sundar62Commented:
Please use the attached java code to perfom what you need. The following would be the command -
java Transform [xmlfile] [xsltfile]

You will have to compile the attached java code.
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
 
public class Transform {
 
    /**
     * Performes an XSLT transformation, sending the results
     * to System.out.
     */
    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println(
                "Usage: java Transform [xmlfile] [xsltfile]");
            System.exit(1);
        }
 
        File xmlFile = new File(args[0]);
        File xsltFile = new File(args[1]);
 
        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xsltFile);
 
        TransformerFactory transFact =
                TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);
 
        trans.transform(xmlSource, new StreamResult(System.out));
    }
}

Open in new window

0
 
abelCommented:
> How can I test xslt?
well, there are many ways to do that. I don't know what processor you are using, but one way is to program your test, like sundar62 is showing. Another way is to use a browser (but I have to advice against that, because there are differences in browser parsing.

If you are planning to use XSLT from the server side, I can advice you to switch to XSLT 2.0. It is well supported through Saxon (http://www.saxonica.com) and several Eclipse plugins (I use the not-free but cheap oXygen plugin) are available can make testing a breeze.

oXygen also comes with a standalone version, for the case that you are not familiar with Eclipse.

If you use Visual Studio, you can do XSLT debugging out of the box, but only for XSLT 1.0

Saxon (6.5 for XSLT 1.0 and 9.x for XSLT 2.0) can be called from the commandline, you do not need a java application to help you out with testing.

-- Abel --
0
 
ZURINETAuthor Commented:
Hi Able

Thanks for your help

I have Visual Studio 2005..

I will try it out..

Thanks alot

Regards
0
 
abelCommented:
You're welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.