[xsl] Re: Transformation of hierarchy, changing levels, and... back

Subject: [xsl] Re: Transformation of hierarchy, changing levels, and... back
From: Marcin Miłkowski <milek_pl@xxxxx>
Date: Wed, 03 Aug 2005 17:03:49 +0200
Hi,

sorry for being imprecise. I tried to use my abbreviated notation and
that was a big mistake. However, it seems to me I found a solution.

Are the parenthetical expressions meant to appear in the output
document or are they there to illustrate the relationship between the
elements in the input document and the elements in the output
document?

Yes, exactly this is what I meant.


I'm quoting my stylesheet below (with some irrelevant sections deleted), I hope you can comment on whether I'm using efficient methods or not. I know it still does not look elegant

I have two other stylesheets which use same ids on g element (ids get generated here by <xml:number count="Tu/Tuv/node()" level="any"/>) to merge the target xml back to its source format. They are however XSLT 2.0 because I had to use charmaps to get rid of some unnecessary entity resolution. It almost works on Saxon (there are still minor glitches on the final merging stylesheets).

Thanks for any comments or suggestion.

Cheers,
	Marcin

************************************************

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 version="1.0">

<xsl:output method="xml" encoding="utf-8" indent="no" doctype-public="-//XLIFF//DTD XLIFF//EN"

doctype-system="http://www.oasis-open.org/committees/xliff/documents/xliff.dtd"/>
	
 <xsl:param name="Src">en-US</xsl:param>
 <xsl:param name="Trg"></xsl:param>

<xsl:template match="text()"/>

 <xsl:template match="TRADOStag">
   <xliff version="1.0" xml:lang="en">
   <xsl:element name="file">
   <xsl:attribute name="original">TMX-file.tmx</xsl:attribute>
   <xsl:attribute name="source-language">
	<xsl:value-of select="$Src"/>
    </xsl:attribute>
    <xsl:attribute name="target-language">
           <xsl:value-of select="$Trg"/>
    </xsl:attribute>
    <xsl:attribute name="datatype">TTX document</xsl:attribute>
    <header>
    <skl>
    <external-file href="skeleton.skl" />
    </skl>
    </header>
    <xsl:choose>
     <xsl:when test="//Tu"/>
      <xsl:otherwise>
      <xsl:message terminate="yes">
        Error: This TTX file is not bilingual, cannot proceed!
        You have to use a monolingual filter with a segmenter.
      </xsl:message>
    </xsl:otherwise>
    </xsl:choose>
   <body>
          <xsl:apply-templates/>
   </body>
     </xsl:element>
  </xliff>
</xsl:template>

 <xsl:template match="//Tu">
   <xsl:element name="trans-unit">
    <xsl:attribute name="id">
         <xsl:value-of select="generate-id()"/>
    </xsl:attribute>
	<xsl:apply-templates/>
   </xsl:element>
</xsl:template>

<xsl:template match="//Tuv">
<xsl:if test="@Lang=$Src">
<source>
	<xsl:apply-templates/>
</source>
</xsl:if>
 <xsl:if test='@Lang=$Trg'>
	<xsl:element name="target">
	
	 <xsl:apply-templates/>
	
	</xsl:element>
</xsl:if>	
 <xsl:if test='@Lang!=$Src and @Lang!=$Trg'>
	<xsl:element name="alt-trans">
		<xsl:element name="target">
		<xsl:attribute name="xml:lang"><xsl:value-of select="@lang"/>
		</xsl:attribute>
		<xsl:apply-templates/>
		</xsl:element>
	</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template match="//Tu/Tuv/df">
<xsl:element name="g">
		<xsl:attribute name="id">
			<xsl:value-of select="generate-id()"/>
		</xsl:attribute>
		<xsl:attribute name="ts">
			<xsl:for-each select="@*">
				<xsl:text>x-df-contents:</xsl:text>
				<xsl:value-of select="name(.)"/>
				<xsl:text>=</xsl:text><xsl:value-of select="."/>
				<xsl:text>;</xsl:text>
			</xsl:for-each>
		</xsl:attribute>
	</xsl:element>
	<xsl:apply-templates/>
	<xsl:element name="g">
		<xsl:attribute name="ts">x-skl-snip</xsl:attribute>
		<xsl:attribute name="id">
		<xsl:number count="Tu/Tuv/node()" level="any"/>
		</xsl:attribute>
	</xsl:element>
		<xsl:value-of select="./text()"/>
		
	<xsl:element name="g">
	<xsl:attribute name="id">
			<xsl:value-of select="generate-id()"/>
		</xsl:attribute>
	</xsl:element>
</xsl:template>

<xsl:template match="//Tu/Tuv/ut | //Tu/Tuv/df/ut">
	<xsl:element name="ph">
		<xsl:attribute name="id">
			<xsl:value-of select="generate-id()"/>
		</xsl:attribute>
		<xsl:value-of select="./text()"/>
	</xsl:element>
</xsl:template>

<xsl:template match="//Tu/Tuv/text()">
<xsl:element name="g">
<xsl:attribute name="ts">x-skl-snip</xsl:attribute>
<xsl:attribute name="id">
	<xsl:number count="Tu/Tuv/node()" level="any"/>
	</xsl:attribute>
</xsl:element>
	<xsl:value-of select="."/>
</xsl:template>


</xsl:stylesheet>


Current Thread