Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - nested xsl:for-each with document function

 
Thread Tools Search this Thread
Old 09-10-2009, 12:39 PM   #1
Default nested xsl:for-each with document function


Hello,

I want to do a nested xsl:for-each with the document function.

Here is a code snippet but it is not working:

<xsl:variable name="cc" select="lower-case(../code-2)"/>
<xsl:variable name="names_DA" select="document('export.xml')/
translation/record[iso.code2=$cc]"/>
<xsl:variable name="lgs" select="lg.vers"/>
<xsl:for-each select="$names_DA">
<xsl:for-each select="$lgs">
<xsl:choose>
<xsl:when test="@lg=$names_DA/source.lang"/>
<xsltherwise>
<xsl:element name="lg.vers">
<xsl:attribute name="lg"><xsl:value-of select="$names_DA/
source.lang"/></xsl:attribute>
<xsl:value-of select="$names_DA/short.name"/>
</xsl:element>
</xsltherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>

The problem I have is that I have to run through all entries of the
external document (with restriction) and afterwards check the other
restrictions in the second for-each. I want to check if the entries
already exists and if not I have to add them out of the external
docoument.

if I use this one:
<xsl:value-of select="$names_DA/short.name"/>
I get entries but it returns the complete records and not only the
current one of the for-each loop.

Hope this explains the problem. Would be happy for any help!

Thanks a lot.
Cozmo



Cozmo
  Reply With Quote
Old 09-10-2009, 12:54 PM   #2
Martin Honnen
 
Posts: n/a
Default Re: nested xsl:for-each with document function
Cozmo wrote:
> Hello,
>
> I want to do a nested xsl:for-each with the document function.
>
> Here is a code snippet but it is not working:
>
> <xsl:variable name="cc" select="lower-case(../code-2)"/>
> <xsl:variable name="names_DA" select="document('export.xml')/
> translation/record[iso.code2=$cc]"/>
> <xsl:variable name="lgs" select="lg.vers"/>
> <xsl:for-each select="$names_DA">
> <xsl:for-each select="$lgs">
> <xsl:choose>
> <xsl:when test="@lg=$names_DA/source.lang"/>
> <xsltherwise>
> <xsl:element name="lg.vers">
> <xsl:attribute name="lg"><xsl:value-of select="$names_DA/
> source.lang"/></xsl:attribute>
> <xsl:value-of select="$names_DA/short.name"/>
> </xsl:element>
> </xsltherwise>
> </xsl:choose>
> </xsl:for-each>
> </xsl:for-each>
>
> The problem I have is that I have to run through all entries of the
> external document (with restriction) and afterwards check the other
> restrictions in the second for-each. I want to check if the entries
> already exists and if not I have to add them out of the external
> docoument.
>
> if I use this one:
> <xsl:value-of select="$names_DA/short.name"/>
> I get entries but it returns the complete records and not only the
> current one of the for-each loop.
>
> Hope this explains the problem. Would be happy for any help!


So you have two XML input documents and want to process them and compare
nodes. Please show us relevant sample of the two documents and explain
the XML output you want the stylesheet to create for those two samples.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/


Martin Honnen
  Reply With Quote
Old 09-10-2009, 01:20 PM   #3
Cozmo
 
Posts: n/a
Default Re: nested xsl:for-each with document function
On Sep 10, 1:54*pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> Cozmo wrote:
> > Hello,

>
> > I want to do a nested xsl:for-each with the document function.

>
> > Here is a code snippet but it is not working:

>
> > <xsl:variable name="cc" select="lower-case(../code-2)"/>
> > * * * * <xsl:variable name="names_DA" select="document('export.xml')/
> > translation/record[iso.code2=$cc]"/>
> > * *<xsl:variable name="lgs" select="lg.vers"/>
> > * *<xsl:for-each select="$names_DA">
> > * * * * * *<xsl:for-each select="$lgs">
> > * * * * * * * * * *<xsl:choose>
> > * * * * * * * * * * * * * *<xsl:when test="@lg=$names_DA/source.lang"/>
> > * * * * * * * * * * * * * *<xsltherwise>
> > * * * * * * * * * * * * * * * * * *<xsl:element name="lg.vers">
> > * * * * * * * * * * * * * * * * * * * * * *<xsl:attribute name="lg"><xsl:value-of select="$names_DA/
> > source.lang"/></xsl:attribute>
> > * * * * * * * * * * * * * * * * * * * * * *<xsl:value-of select="$names_DA/short.name"/>
> > * * * * * * * * * * * * * * * * * *</xsl:element>
> > * * * * * * * * * * * * * *</xsltherwise>
> > * * * * * * * * * *</xsl:choose>
> > * * * * * *</xsl:for-each>
> > * *</xsl:for-each>

>
> > The problem I have is that I have to run through all entries of the
> > external document (with restriction) and afterwards check the other
> > restrictions in the second for-each. I want to check if the entries
> > already exists and if not I have to add them out of the external
> > docoument.

>
> > if I use this one:
> > <xsl:value-of select="$names_DA/short.name"/>
> > I get entries but it returns the complete records and not only the
> > current one of the for-each loop.

>
> > Hope this explains the problem. Would be happy for any help!

>
> So you have two XML input documents and want to process them and compare
> nodes. Please show us relevant sample of the two documents and explain
> the XML output you want the stylesheet to create for those two samples.
>
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


Hello,

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)

Someting like that "c2 = iso.code2 and sourcelang != @lg"

But as I have to loop through all these items its difficult.

Any idea? Hope this explains the problem.

Thanks for helping.

Greets,
Cozmo


Cozmo
  Reply With Quote
Old 09-10-2009, 02:01 PM   #4
Martin Honnen
 
Posts: n/a
Default Re: nested xsl:for-each with document function
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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:strip-space elements="*"/>
<xslutput indent="yes"/>

<xslaram 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/


Martin Honnen
  Reply With Quote
Old 09-10-2009, 02:08 PM   #5
Cozmo
 
Posts: n/a
Default Re: nested xsl:for-each with document function
On Sep 10, 3:01*pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> 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
> * *xmlnssl="http://www.w3.org/1999/XSL/Transform"
> * *version="1.0">
>
> * *<xsl:strip-space elements="*"/>
> * *<xslutput indent="yes"/>
>
> * *<xslaram 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/


Amazing it is working. Wonderful you helped a lot! Quite happy
know

Thanks a lot!!!!


Cozmo
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent function for itoa in Linux gcc compiler suse Software 0 03-06-2009 05:30 AM
Document Management compukid007 Software 0 12-25-2008 05:49 AM
appending xml document in C# kanjali Software 0 04-18-2008 02:31 PM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
File download dialog box is coming while opening the document using Servlet aarthi Software 0 04-24-2007 03:20 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46