Solved

XSLT validation : input to output. Multiple namespaces

Posted on 2009-05-10
7
525 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

747 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

13 Experts available now in Live!

Get 1:1 Help Now