468,525 Members | 2,196 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

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

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)
Jul 20 '05 #1
1 5011
"Marco Leist" <ma*********@online.de> wrote in message
news:c2*************************@posting.google.co m...
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.
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Miguel J. Jiménez | last post: by
2 posts views Thread by Roland Hall | last post: by
4 posts views Thread by Hardy Wang | last post: by
2 posts views Thread by John A Grandy | last post: by
3 posts views Thread by Hvid Hat | last post: by
4 posts views Thread by Ouray Viney | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.