XML and XSLT question. Please help me out...

D

dennis

Hi,

First of all, hi to you all.
I'm working on a Delphi project wich is becoming near it's deadline.
I have a very simple XSLT question wich i hope one of you folks can
help me with?

The problem is i need to transform a xml file from this:

<root>
<metadata>This is some metadata</metadata>
<item>
<b>some text</b>
<c>href://www.somewhere.com?id=001</c>
</item>
<item>
<b>some other text</b>
<c>href://www.somewhere.com?id=002</c>
</item>
</root>

to this:

<root>
<metadata>This is some metadata</metadata>
<item>
<b>some text</b>
<c>href://www.somewhere.com/?var=X&new_id=001</c>
</item>
<item>
<b>some other text</b>
<c>href://www.somewhere.com/?var=X&new_id=002</c>
</item>
</root>

This has to be done using XSLT.
The translation from 'href://www.somewhere.com?id=002' to
'href://www.somewhere.com/?var=X&new_id=002' is no problem, i'll be
using regular expressions for that. The xslt script however is a big
problem (for me that is). Can someone please help me out on this one,
there's no one at my current office who knows any XSLT.. And reading
all the online tutorials (spend one day on that) didn't help me..

Many thanks in advance,
Dennis
 
H

hilz

The translation from 'href://www.somewhere.com?id=002' to
'href://www.somewhere.com/?var=X&new_id=002' is no problem, i'll be
using regular expressions for that.



So if you're going to be using regular expressions for that, what do you
want the xslt script to do? Other than this change you indicated above,
the two trees look identical to me, and would not need any
transformation after you've done what you wanted using regular
expressions. Or am i missing something here?
 
D

dennis

hilz said:
So if you're going to be using regular expressions for that, what do you
want the xslt script to do? Other than this change you indicated above,
the two trees look identical to me, and would not need any
transformation after you've done what you wanted using regular
expressions. Or am i missing something here?

Yes your missing the point that they demand it's an xslt file.
The application is alleady written, one of the demands was that it had
the option of applying an external XSLT file, this way the user of the
application had control over the XML the application outputs.
I've build this option succesfully into my application, but know
they're asking if i can deliver the app with one standard *.xsl file
wich does the above.
The problem being i know to little about XSLT.

The main problem i'm running into here is that most examples on the net
are showing you how to transform the xml entirely. They're not showing
how to keep the original xml only changing one recurring element
(item/c in this case).

I'm looking for a simple example wich does transform the provided xml,
i can than myself hack the XPATH (those are regeps right?) expressions
in the *.xsl file so it matches my client's needs.

Hope someone can help,
Dennis
 
H

hilz

dennis said:
Yes your missing the point that they demand it's an xslt file.
The application is alleady written, one of the demands was that it had
the option of applying an external XSLT file, this way the user of the
application had control over the XML the application outputs.
I've build this option succesfully into my application, but know
they're asking if i can deliver the app with one standard *.xsl file
wich does the above.
The problem being i know to little about XSLT.

The main problem i'm running into here is that most examples on the net
are showing you how to transform the xml entirely. They're not showing
how to keep the original xml only changing one recurring element
(item/c in this case).

I'm looking for a simple example wich does transform the provided xml,
i can than myself hack the XPATH (those are regeps right?) expressions
in the *.xsl file so it matches my client's needs.

Hope someone can help,
Dennis


ok here is a starting point...
you can use something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes" omit-xml-declaration="no"
version="1.0"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="c">
*** here you need to output the transformed href ****
</xsl:template>

</xsl:stylesheet>




this will bascially copy all elements/attributes, except for the <c>
element, where you will need to somehow transform the old href to a new
one...
so all what you need to do is add some code in the place indicated by
this line
*** here you need to output the transformed href ****

HTH
 
P

Peter Flynn

hilz said:
ok here is a starting point...
you can use something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes" omit-xml-declaration="no"
version="1.0"/>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="c">
*** here you need to output the transformed href ****
</xsl:template>

</xsl:stylesheet>




this will bascially copy all elements/attributes, except for the <c>
element, where you will need to somehow transform the old href to a new
one...
so all what you need to do is add some code in the place indicated by
this line
*** here you need to output the transformed href ****

....which would be something like

<xsl:template match="c">
<c>
<xsl:value-of select="substring-before(.,'id=')"/>
<xsl:text>var=X&amp;new_id=</xsl:text>
<xsl:value-of select="substring-after(.,'id=')"/>
</c>
</xsl:template>

No need for any REs, just split the string and insert the new stuff.

///Peter
 
D

dennis

Peter & hilz

THANK YOU !!!!!

This came in just in time, spend allmost all night reading a zillion
tutorials. I found the copy and copy-of functions but didn't get them
to work, till now that is..

Have a nice weekend and again Thanks,
Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top