Hi, I have the following XML file: <TS> <context> <name>ElementName</name> <message> <location line="16" filename="build/debug/xslt/ xml_ApplicationDefaults.cpp" /> <source>Application</source> <translation>Programm</translation> </message> <message> <location line="24" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Archive hierarchy</source> <translation type="unfinished" /> </message> <message> <location line="18" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Folder</source> <translation>Ordner</translation> </message> <message> <location line="28" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Job assembler</source> <translation type="unfinished" /> </message> <message> <location line="26" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Job dismantler</source> <translation>Jobs zerlegen</translation> </message> </context> </TS> To process this file in our translation environment I would need an XSL transformation that copies this file with the following change: whenever the attribute type of the element translation equals unfinished, I want the content of the element source copied to the element target. So the desired output would look like this: <TS> <context> <name>ElementName</name> <message> <location line="16" filename="build/debug/xslt/ xml_ApplicationDefaults.cpp" /> <source>Application</source> <translation>Programm</translation> </message> <message> <location line="24" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Archive hierarchy</source> <translation type="unfinished">Archive hierarchy</translation> </message> <message> <location line="18" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Folder</source> <translation>Ordner</translation> </message> <message> <location line="28" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Job assembler</source> <translation type="unfinished">Job assembler</translation> </message> <message> <location line="26" filename="build/debug/xslt/ xml_ElementsPaneLayout_PS.cpp" /> <source>Job dismantler</source> <translation>Jobs zerlegen</translation> </message> </context> </TS> I recycled a previous XSLT but this doesn't quite produce the right output: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output indent="yes" encoding="UTF-8"/> <xsl:template match="*"> <xsl:call-template name="createNode"/> </xsl:template> <xsl:template name="createNode"> <xsl:variable name="tagName"> <xsl:value-of select="local-name()"/> </xsl:variable> <xsl:choose> <xsl:when test="$tagName = 'translation'"> <xsl:element name="translation"> <xsl:attribute name="type"> <xsl:value-of select="./@type"/> </xsl:attribute> <xsl:value-of select="../ source"/> <xsl:for-each select="./*"> <xsl:call-template name="createNode"/> </xsl:for-each> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:element name="{$tagName}"> <xsl:if test="count(./*) = 0"> <xsl:value-of select="."/> </xsl:if> <xsl:for-each select="./*"> <xsl:call-template name="createNode"/> </xsl:for-each> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> Thanks! Thomas
Thomas wrote: > To process this file in our translation environment I would need an > XSL transformation that copies this file with the following change: > whenever the attribute type of the element translation equals > unfinished, I want the content of the element source copied to the > element target. > > > So the desired output would look like this: > > > <TS> > <context> > <name>ElementName</name> > <message> > <location line="16" filename="build/debug/xslt/ > xml_ApplicationDefaults.cpp" /> > <source>Application</source> > <translation>Programm</translation> > </message> > <message> > <location line="24" filename="build/debug/xslt/ > xml_ElementsPaneLayout_PS.cpp" /> > <source>Archive hierarchy</source> > <translation type="unfinished">Archive hierarchy</translation> I don't see any element named 'target' in the output, rather the contents of the 'source' element is copied to the 'translation' element. The following stylesheet does that: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="message/translation[@type = 'unfinished']"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:value-of select="../source"/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Hi, Sorry, you're right, I meant the 'translation' element instead of 'target' element. Thanks a lot for the quick response. Works like a charm. Thomas On 11 jul, 13:41, Martin Honnen wrote: > Thomas wrote: > > To process this file in our translation environment I would need an > > XSL transformation that copies this file with the following change: > > whenever the attribute type of the element translation equals > > unfinished, I want the content of the element source copied to the > > element target. > > > So the desired output would look like this: > > > <TS> > > <context> > > <name>ElementName</name> > > <message> > > <location line="16" filename="build/debug/xslt/ > > xml_ApplicationDefaults.cpp" /> > > <source>Application</source> > > <translation>Programm</translation> > > </message> > > <message> > > <location line="24" filename="build/debug/xslt/ > > xml_ElementsPaneLayout_PS.cpp" /> > > <source>Archive hierarchy</source> > > <translation type="unfinished">Archive hierarchy</translation> > > I don't see any element named 'target' in the output, rather the > contents of the 'source' element is copied to the 'translation' element. > > The following stylesheet does that: > > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > > <xsl:template match="@* | node()"> > <xsl:copy> > <xsl:apply-templates select="@* | node()"/> > </xsl:copy> > </xsl:template> > > <xsl:template match="message/translation[@type = 'unfinished']> <xsl:copy> > <xsl:apply-templates select="@*"/> > <xsl:value-of select="../source"/> > </xsl:copy> > </xsl:template> > > </xsl:stylesheet> > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/- Tekst uit oorspronkelijk bericht niet weergeven - > > - Tekst uit oorspronkelijk bericht weergeven -