Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: nested xsl:for-each with document function >Thread Next - Re: nested xsl:for-each with document function Re: nested xsl:for-each with document functionTo: NULL Date: 9/10/2009 3:01:00 PM Cozmo wrote:
> No I have one input document and want to transform it via saxon. But I
> need data from a second one so I use the document function. (merge the
> information)
>
> First input file:
>
> <record>
> <c2>AD</c2>
> <c3>AS</c3>
> <cn>002</cn>
> <short.name>
> <lg.vers lg="ss">text</lg.vers>
> <lg.vers lg="tt">text</lg.vers>
> <lg.vers lg="ts">text</lg.vers>
> ....
>
>
> the file which I import via the document function:
>
> <record>
> <short.name>test</short.name>
> <long.name>text</long.name>
> <iso.code2>AD</iso.code2>
> <source.lang>bg</source.lang>
> </record>
> <record>
> <short.name>test</short.name>
> <long.name>text</long.name>
> <iso.code2>AD</iso.code2>
> <source.lang>tt</source.lang>
> </record>
> <record>
> <short.name>test</short.name>
> <long.name>text</long.name>
> <iso.code2>DZ</iso.code2>
> <source.lang>bg</source.lang>
> </record>
>
> In the second file are additional information which I want to Add as a
> node. Example:
> <lg.vers lg="bg">test</lg.vers> should be added.
>
> As you can see first restriction c2 = iso.code2.
> Second restriction add elemnt only if source.lang doesnt exist list.
> ( So, @lg = tt shouldn't be added)
Assuming the primary input is
<root>
<record>
<c2>AD</c2>
<c3>AS</c3>
<cn>002</cn>
<short.name>
<lg.vers lg="ss">text</lg.vers>
<lg.vers lg="tt">text</lg.vers>
<lg.vers lg="ts">text</lg.vers>
</short.name>
</record>
</root>
and the second XML document is
<root>
<record>
<short.name>test</short.name>
<long.name>text</long.name>
<iso.code2>AD</iso.code2>
<source.lang>bg</source.lang>
</record>
<record>
<short.name>test</short.name>
<long.name>text</long.name>
<iso.code2>AD</iso.code2>
<source.lang>tt</source.lang>
</record>
<record>
<short.name>test</short.name>
<long.name>text</long.name>
<iso.code2>DZ</iso.code2>
<source.lang>bg</source.lang>
</record>
</root>
then this stylesheet copies everything from the primary input document
and in the short.name element of each record elements adds new lg.vers
elements:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:param name="sd" select="'test2009091002.xml'"/>
<xsl:variable name="srs" select="document($sd)/root/record"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="short.name">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:apply-templates select="$srs[iso.code2 = current()/../c2 and
not(source.lang = current()/lg.vers/@lg)]" mode="new"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record" mode="new">
<lg.vers lg="{source.lang}"><xsl:value-of
select="short.name"/></lg.vers>
</xsl:template>
</xsl:stylesheet>
creates the output
<root>
<record>
<c2>AD</c2>
<c3>AS</c3>
<cn>002</cn>
<short.name>
<lg.vers lg="ss">text</lg.vers>
<lg.vers lg="tt">text</lg.vers>
<lg.vers lg="ts">text</lg.vers>
<lg.vers lg="bg">test</lg.vers>
</short.name>
</record>
</root>
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
