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

Discussion in 'XML' started by dennis, Oct 27, 2005.

  1. dennis

    dennis Guest

    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
    dennis, Oct 27, 2005
    #1
    1. Advertising

  2. dennis

    hilz Guest


    > 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?
    hilz, Oct 27, 2005
    #2
    1. Advertising

  3. dennis

    dennis Guest

    hilz wrote:
    > > 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?


    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
    dennis, Oct 27, 2005
    #3
  4. dennis

    hilz Guest

    dennis wrote:
    > hilz wrote:
    >
    >>>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?

    >
    >
    > 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
    hilz, Oct 27, 2005
    #4
  5. dennis

    Peter Flynn Guest

    hilz wrote:

    > dennis wrote:
    >> hilz wrote:
    >>
    >>>>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?

    >>
    >>
    >> 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 ****


    ....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
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Oct 27, 2005
    #5
  6. dennis

    dennis Guest

    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

    Peter Flynn wrote:
    > hilz wrote:
    >
    > > dennis wrote:
    > >> hilz wrote:
    > >>
    > >>>>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?
    > >>
    > >>
    > >> 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 ****

    >
    > ...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
    > --
    > XML FAQ: http://xml.silmaril.ie/
    dennis, Oct 28, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    621
    Stylus Studio
    Aug 3, 2004
  2. Martijn
    Replies:
    3
    Views:
    372
    Micah Cowan
    Sep 26, 2003
  3. Replies:
    4
    Views:
    648
  4. KK
    Replies:
    2
    Views:
    497
    Big Brian
    Oct 14, 2003
  5. jkflens
    Replies:
    2
    Views:
    1,437
    jkflens
    May 30, 2006
Loading...

Share This Page