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

M

Marco Leist

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)
 
M

Mike Brown

Marco Leist said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top