nested xsl:for-each with document function

C

Cozmo

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"/>
<xsl:eek:therwise>
<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>
</xsl:eek:therwise>
</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
 
M

Martin Honnen

Cozmo said:
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"/>
<xsl:eek:therwise>
<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>
</xsl:eek:therwise>
</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.
 
C

Cozmo

Cozmo said:
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"/>
                           <xsl:eek:therwise>
                                   <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>
                           </xsl:eek:therwise>
                   </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.

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
 
M

Martin Honnen

Cozmo said:
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:eek:utput 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>
 
C

Cozmo

Cozmo said:
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:eek:utput 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>

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

Thanks a lot!!!!
 
Joined
Mar 20, 2010
Messages
1
Reaction score
0
hello, and if I have an xml document with variables, for example like this ...

<root>
> * * <record>
> * * * * <C2> AD = 1 </ c2>
> * * * * <c3> AS = 2 </ c3>
> * * </ Record>
> </ Root>

is possible with the XSLT document() function to take the values 1 and 2?

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top