Solved

XSLT validation : input to output. Multiple namespaces

Posted on 2009-05-10
7
530 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 …
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

770 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