XSL : How can i replace text in XML-Documents ???

Discussion in 'XML' started by Marco Leist, Aug 6, 2003.

  1. Marco Leist

    Marco Leist Guest

    hello,

    i´d like to replace certain strings in a XML-Document with other ones.
    Thereby the structure (tags)of the source-xml should be preserved
    completly, the strings that should be replaced are values of different
    non-pre-defined tags.

    What i try to say is that the whole thing should work in a generic
    way, independant from the DTD of the source-xml (like a
    search-replace-function of a text-editor would do) - i don´t want to
    hard-code tag-names in template-matches...



    Example :

    <source>
    ....any xml tags....
    <tagX>Seife</tagX>
    <tagY>Kaffee</tagY>
    ....any other xml tags....
    </source>

    <destination>
    ....any xml tags....
    <tagX>Soap</tagX>
    <tagY>Coffee</tagY>
    ....any other xml tags....
    </destination>

    The "Lookup" should be defined in another Structure, possibly in
    another XML-File, e.g. :

    <lookup>
    <elem>
    <de>Seife</de>
    <en>Soap</en>
    </elem>
    ...and so on....
    </lookup>


    Thanks a lot for your help !

    Marco
    (from Germany)
     
    Marco Leist, Aug 6, 2003
    #1
    1. Advertising

  2. Marco Leist

    Mike Brown Guest

    Re: How can i replace text in XML-Documents ???

    "Marco Leist" <> wrote in message
    news:...
    > hello,
    >
    > i´d like to replace certain strings in a XML-Document with other ones.
    > Thereby the structure (tags)of the source-xml should be preserved
    > completly, the strings that should be replaced are values of different
    > non-pre-defined tags.
    >
    > What i try to say is that the whole thing should work in a generic
    > way, independant from the DTD of the source-xml (like a
    > search-replace-function of a text-editor would do) - i don´t want to
    > hard-code tag-names in template-matches...
    >
    >
    >
    > Example :
    >
    > <source>
    > ...any xml tags....
    > <tagX>Seife</tagX>
    > <tagY>Kaffee</tagY>
    > ...any other xml tags....
    > </source>
    >
    > <destination>
    > ...any xml tags....
    > <tagX>Soap</tagX>
    > <tagY>Coffee</tagY>
    > ...any other xml tags....
    > </destination>
    >
    > The "Lookup" should be defined in another Structure, possibly in
    > another XML-File, e.g. :
    >
    > <lookup>
    > <elem>
    > <de>Seife</de>
    > <en>Soap</en>
    > </elem>
    > ...and so on....
    > </lookup>


    It can be done. Investigate the use of document(), <xsl:copy/>, <xsl:key/>
    and key(). You will need a template that matches "*". It will use key() to
    see if there is a match in the lookup file for the current node's
    string-value. When there's a match, you'll use <xsl:copy><xsl:value-of
    select="key('lookuptable',$currentstring)"/></xsl:copy> (). Otherwise,
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> (identity
    transform). Using key() with external files requires using xsl:for-each
    select="document('lookup.xml')" to temporarily change the context, which is
    why you need to create $currentstring...

    This explanation probably doesn't make sense to you, but someone else can
    probably show you the code.. it's not that hard once you see it.
     
    Mike Brown, Aug 6, 2003
    #2
    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. Replies:
    1
    Views:
    3,637
    A. Bolmarcich
    May 27, 2005
  2. François Robert
    Replies:
    3
    Views:
    688
    Toni Uusitalo
    Jul 19, 2005
  3. wyattroerb
    Replies:
    0
    Views:
    272
    wyattroerb
    May 2, 2006
  4. Replies:
    1
    Views:
    493
    Juan T. Llibre
    Oct 18, 2006
  5. Replies:
    4
    Views:
    784
    Dimitre Novatchev
    Nov 24, 2006
Loading...

Share This Page