Solved

XSLT validation : input to output. Multiple namespaces

Posted on 2009-05-10
7
527 Views
Last Modified: 2013-11-18
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


0
Comment
Question by:ZURINET
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:sundar62
ID: 24348813
Please check the attached link -
http://www.xml.com/pub/a/2001/04/04/trxml/index.html
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24348854
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
 

Author Comment

by:ZURINET
ID: 24348993
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:sundar62
ID: 24349017
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
 
LVL 39

Expert Comment

by:abel
ID: 24349080
> 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
 

Author Comment

by:ZURINET
ID: 24349393
Hi Able

Thanks for your help

I have Visual Studio 2005..

I will try it out..

Thanks alot

Regards
0
 
LVL 39

Expert Comment

by:abel
ID: 24351530
You're welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
create wsdl for check policy price 32 32
Consuming WCF service from Java with callback contract 5 256
Google adds suggestions 3 58
What language/protocol is the Angular Chat? 2 57
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now