[xsl] Re: Complicated transformation step when using localization

Subject: [xsl] Re: Complicated transformation step when using localization
From: knocte <knocte@xxxxxxxxx>
Date: Thu, 13 Oct 2005 10:11:32 +0200
knocte wrote:
Hello list!
I am trying to make a transformation step but I can't get it to work!

I will receive an XML document with many nodes. Some of them may contain the attribute "translate-attrs":

<...>
<img src="myURI" alt="translate me" title="me too" translate-attrs="alt title" />
<...>


I want to select all the nodes that have this attribute and replace the content of the attributes with the content of an external file, like this:

<external-dictionary>
 <text key="translate me">Translate me, in other language</text>
 <text key="me too">Me too, in other idiom</text>
</external-dictionary>

Expected final XML result would be:

<...>
<img src="myURI" alt="Translate me, in other language" title="Me too, in other idiom" />
<...>



So first we want to match all the nodes that have this attribute:


<xsl:template match="*[@translate-attrs]">


Then we want to copy all attributes except the one mentioned:


<xsl:copy-of select="@*[not (local-name()='translate-attrs')]" />

Now I want to do a for-each part which captures all of the terms separated by spaces that are contained in the translate-attrs attribute, and make an attribute by each, with the content of the external XML file, but I can't achieve it! It would be something like:

<xsl:for-each select"@translate-attrs">
<xsl:attribute name=".">
<xsl:value-of select="document($myxml)//external-dictionary/text[@key = current()/@{.}]" />
</xsl:attribute>
</xsl:for-each>


Finally, we need to copy the rest of the child elements:

 <xsl:apply-templates />
</xsl:template>


Does anybody know how can I make the middle part, the most difficult one? Any help will be appreciated!


Regards,

Andrew [ knocte ]

After some more research I think I must use a combination of two techniques:


a) Recursive named template (example: "String split into elements" -> http://www.dpawson.co.uk/xsl/sect2/N7240.html#d9570e343 ).
b) <xsl:key> (example: "Replacing a string by another which is found by reference in the same XML document" -> http://www.dpawson.co.uk/xsl/sect2/N7240.html#d9570e109 ).


Am I correct?

Thanks in advance.

Andrew [ knocte ]

--

Current Thread