aspose file tools*
The moose likes XML and Related Technologies and the fly likes Problem creating XSLT using XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem creating XSLT using XML" Watch "Problem creating XSLT using XML" New topic
Author

Problem creating XSLT using XML

Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
I want to transform the following XML :


into the following XML




I have written the follwoing XSLT (which does not work):



Please tell me what is worng with my XSLT
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, let me first point out that the XML you say you want to produce is actually not well-formed. It has a start tag which doesn't match its end tag.

But as for why it "doesn't work", it's impossible to say why that might be without knowing what it is supposed to do. All you have done is to show us one small example. I could write an XSL transformation which did nothing but ignore the input and write your desired output by hard-coding it. But that isn't what you want -- there are some rules for what you really want, aren't there? So tell us what they are.

Also if "doesn't work" means that something crashes, you should let us know. Right now I'm guessing that the transformation just doesn't produce the correct output, but you didn't say that. So if that wasn't what "doesn't work" meant, tell us what it does mean.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Edited and corrected.

There are some assumptions like
1. <vs> can be many.
2. based on the name attributes value ('_uncommon') transformation will happen.
etc.

The problem is the transformation is not happening.

Please check my XSLT syntax and tell me if there are any syntax errors.
Is the following line of code

<xsl:if test=" v[@name != '_common']">

syntaxtically correct ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Tiya Khambadkone wrote:Please check my XSLT syntax and tell me if there are any syntax errors.


The transformer will tell you that itself, by throwing an exception. Is your code written with catch-blocks which ignore exceptions?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And once you get your transformation running, I think you'll find that it always produces a root element like this:



Which isn't what your original post's example looked like.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
I am using web browser (Internet explorer) as a transformer. We can use IE to transform right?
Do i just need to open my xml giving the xslt link in it (in order to transform the xml)?
I tried doing it and I did not get any error message (got only a blank web browser page).

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Tiya Khambadkone wrote:I am using web browser (Internet explorer) as a transformer. We can use IE to transform right?


Yes, although I never tried that myself. But I would suggest testing your transformation elsewhere before you deploy it, as it looks like IE just does nothing if there are errors in it.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Do you know any tool to transform XML to another XML using XSLT ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You mean some software product which allows you to enter the XML and XSLT and try the transformation? As I understand it there's lots of them. (I haven't used any of them.) Your google keywords are xslt editor if you want to try some. However if you aren't going to write XSLT on a regular basis you might not want to pay the money for one of those products. In which case you can just write some Java code which does the transformation. More clumsy and less user-friendly but also cheaper.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
can someone tell me if my if condition syntax in xslt is correct ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Paul Clapham wrote:You mean some software product which allows you to enter the XML and XSLT and try the transformation? As I understand it there's lots of them. (I haven't used any of them.)


Actually I just remembered, that isn't true. I have in the past used Kernow, which is (as its home page says) "a tool designed to make it faster and easier to repeatedly run XSLT transforms". It's open-source and free.
E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
Tiya Khambadkone wrote:Do you know any tool to transform XML to another XML using XSLT ?


Ive been working with XML Copy Editor by source forge for testing transformations before setting up the programming. http://xml-copy-editor.sourceforge.net/
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
Please tell me what is worng with my XSLT

Since there seems to have some mismatch between the intended output (such as the key="mess": the intention should be key="disp", I would guess) and what reflects from the xslt (rightly or wrongly scripted), I can only rely on some reasonable extrapolation of the intent by guessing in the following corrected version. No guarantee.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Thank you very much. It works . The value of usage is printed.
But I need the output to be displayed in XML format.
I do not understand why I get only the value of usage.
Why not the tags like <extensions> are printed?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
I do not understand why I get only the value of usage.

If you have issues with the "rendering" or displaying of an xml document, it depends quite a bit on the system and setup and on the origin of the document.

If the document is fetched from a local file system, then, on windows at least, it depends on the file (extension) association on the one hand and the user-agent on the other. Say if you display it on a browser and the local file is .htm or .html (not .xml) then normally the browser would display the "value of usage" whatever it means, tags are ignored as it does not understand them.

If the document is fetched via http with again a browser, it depends on the header being served. If that's served with mime type text/html, then you again would have some display problem even you can still view source to see the whole thing.

So the problem probably lies elsewhere and you can look into those directions mentioned.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Thanks for the information.

Also I wanted to know , as in your post you have mentioned that you have assumed the 'key' as 'disp' instead of 'mess'. Actually I want the 'key' as 'mess'. Do you know how can we do that ?

Instead of

<xsl:value-of select="$name" />

I tried using

<xsl:value-of select="../../e[@name]" />

It does not work. How can I do that ?

Also do you know if I need a counter on for loop, how can I add it and use it ?

Something like

<xsl:for-each select="e/ext[@name=$name]/use" start="1">

and use it as something like

<xsl:value-of select="{concat($name,'$start')}" />
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't understand your first question, so I'm going to skip over that.

As for your second question, it looks to me like you're asking about the <xsl:number> element.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
Last thing first.
<xsl:value-of select="{concat($name,'$start')}" />

This is guaranteed to be an error. You don't put brace freely like that. You've to study why I put somewhere {} in my posted xslt. It appears in specific places where it is needed to work as "avt", as it is technically called, "attribute value template". You cannot use it like that in the xsl:value-of element select attribute. It is an error.

Then, I don't really understand your intention. If it is "mess" there, then I can only deduce disps tag and disp tag are fixed and rigid independent of the ext element's name attribute. It would be a simpler xslt. But you seem not to be very careful in posting the question in the first place: why there is location attribute at some places and loc attribute in some others comparable positions??? Can I take it all as location (or loc, if you like)?

Here is the version taking "disp" or "mess" as a parameter. In this more general rendering, it can be inputted by the application.

Anything I have not touched upon is that I don't understand and I can't help.
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
Thanks. I will try this.

Also, thanks for the quick reply. Your reply is very helpful.

2 more questions :

1.If I want to transform the following xml snippet :

<e name="disc">
<ext name="aV">
<use loc="IN" language="es" >test1</use>
<use loc="ME" language="es" >test2</use>
<use loc="IN,ME" language="es" >test3</use>
<use loc="IN,ME" language="es" >test4</use>
</ext>
</e>

into the following xml

<ext>
<opts>
<cont zip="IN">
<opt key="opt1">text1</opt>
</cont>
<cont zip ="ME">
<opt key="opt2">text2</opt>
</cont>
<cont zip ="IN,ME">
<opt key="opt3">test3</opt>
<opt key="opt4">test4</opt>
</cont>
</opts>
</ext>

then how will the xslt look like ?

I understand that I need to use a for loop , parse , map and group based on 'loc' value.
But i dont know how can I group these based on loc? here loc maps to zip.

2.
My original XML file :

<?xml-stylesheet type="text/xsl" href="m1.xsl"?>
<vs version="0.00">
<v name="_uncommon">
<e name="page">
<ext name="mess">
<use loc="CA">text1</use>
<use loc="CA">text2</use>
</ext>
</e>

<e name="gui">
<ext name="disp">
<use location="CA">text</use>
</ext>
</e>
</v>
</vs>

If I want to transform the above xml file into different xml files based on the '<e>' (meaning for every <e> there should be a different xml file), then how can this be achieved?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
[0]
I will try this.

I hope this is not a question you relay on behalf of other developers. That I wouldn't take it too gracefully. The try would not take minutes if it is your project.

[1]
If I want to transform the following xml snippet :

into the following xml

then how will the xslt look like ?
I understand that I need to use a for loop , parse , map and group based on 'loc' value.
But i dont know how can I group these based on loc? here loc maps to zip.

I have no idea on what you had in mind. In any case, that is not my understanding based on your understanding whatever it means.

In any case, I am quite willing to help one more time, probably the last in this context. I am happy to help to the last iota if I think that helps members to do their study based on the concrete and focused demo, but that I would stop quick if I think that does not lead to the intended effect. This is how it can look like.

[2]
My original XML file :

If I want to transform the above xml file into different xml files based on the '<e>' (meaning for every <e> there should be a different xml file), then how can this be achieved?

You might think xslt is something like cookbook, consisting a portfolio of recipes. That can not be more misconceived. I leave it or any variants of it to your own otherwise my previous efforts would be counter-productive and could then be criticised with good reason for taking on one's own other's work. Sorry, I can't help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I assume you are using XSLT 1.0. In which case "grouping" is an FAQ and your web search keywords are xslt grouping. When I do that, the first link I get back is this one: Grouping.

And just to reiterate what g tsuji said, have a look at the Ranch's FAQ entries NotACodeMill and ShowSomeEffort.

Unfortunately XSLT seems to be one of those technologies where people think that since it isn't going to be a major part of their work, they don't have to spend much time learning it and they can just hack together the first thing that works. (That was how I thought of CSS, I now realize, and I find that what I did with CSS over the last few years isn't very good.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem creating XSLT using XML