?
Solved

XSLT validation : input to output. Multiple namespaces

Posted on 2009-05-10
7
Medium Priority
?
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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