[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Key and document() problem ?


Subject: Re: [xsl] Key and document() problem ?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 07 Oct 2008 12:37:13 -0400

At 2008-10-07 17:43 +0200, Emilise Victor wrote:
I would like to construct a key based on a different document, but I saw that is was not going to work. So I'm going explain what I want to do, since now I have no idea how it could be done.
Here are two pieces of the document I'm working on :
...
The whole thing is about selecting the requests of the 2nd document according to their step in the first document. So, usually it was easy since I had a for-each loop for the steps. The "step" level was so preserved. ie, I calculate the sum of request/data/@value in each step. That would be 2394 for step 1 and 1969+5451 for step2.
Now my problem is that I don't have the for-each step loop, but I still have to sum request according to the step they are in.

You don't explain why you don't have the for-each loop ... and you don't explain your rationale for error codes. And you don't indicate if you are using XSLT 1 or XSLT 2. So I'm not sure if I can help.


I don't know if it's clear. I don't even see clearly myself how I could achieve this.

The example below shows the access to two different documents producing a report of the comparison of information. I'm not sure if it will help you or not.


I'm not sure why you weren't using variables in your example, so in the below I have code without and then with variables, and I'm using XSLT 1 assuming you are since you didn't mention XSLT 2.

BTW, you don't have to worry about calling the document() function multiple times because the processor is supposed to fetch the document only once.

I hope this helps.

. . . . . . . . . . Ken


T:\ftemp>type job.xml <job> <universe> <monitor id="45995" timeout="10000"> <step id="1" timeout="4000"> <request id="1"/> </step> <step id="2" timeout="9000"> <request id="2"/> <request id="3"/> </step> <step id="3" timeout="6000"> <request id="2"/> <request id="3"/> </step> </monitor> </universe> </job>

T:\ftemp>type monitor.xml
<monitor id="45995">
        <site>
                <test id="36">
                        <request id="1">
                                <data id="4" value="2394"/>
                        </request>
                        <request id="2">
                                <data id="4" value="1969"/>
                        </request>
                        <request id="3">
                                <data id="4" value="5451"/>
                        </request>
                </test>
        </site>
</monitor>

T:\ftemp>call xslt victor1.xsl victor1.xsl victor1.out

T:\ftemp>type victor1.out
Step 1: (1) 2394  NO TIMEOUT [4000]
Step 2: (2) 7420  NO TIMEOUT [9000]
Step 3: (2) 7420  TIMEOUT [6000]

T:\ftemp>call xslt victor2.xsl victor2.xsl victor2.out

T:\ftemp>type victor2.out
Step 1: (1) 2394  NO TIMEOUT [4000]
Step 2: (2) 7420  NO TIMEOUT [9000]
Step 3: (2) 7420  TIMEOUT [6000]

T:\ftemp>type victor1.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:variable name="job">job.xml</xsl:variable>
<xsl:variable name="monitor">monitor.xml</xsl:variable>

<xsl:template match="/">
<xsl:for-each select="document($job)/job/universe/monitor/step">
<xsl:text/>Step <xsl:value-of select="@id"/>: (<xsl:text/>
<xsl:value-of select="count(document($monitor)/monitor/site/test/request
[@id=current()/request/@id])"/>
<xsl:text>) </xsl:text>
<xsl:value-of select="sum(document($monitor)/monitor/site/test/request
[@id=current()/request/@id]/data/@value)"/>
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="sum(document($monitor)/monitor/site/test/request
[@id=current()/request/@id]/data/@value) > @timeout">
<xsl:text>TIMEOUT</xsl:text>
</xsl:when>
<xsl:otherwise>NO TIMEOUT</xsl:otherwise>
</xsl:choose>
<xsl:text/> [<xsl:value-of select="@timeout"/><xsl:text>]
</xsl:text>
</xsl:for-each>
</xsl:template>


</xsl:stylesheet>
T:\ftemp>type victor2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:variable name="job">job.xml</xsl:variable>
<xsl:variable name="monitor">monitor.xml</xsl:variable>

<xsl:template match="/">
  <xsl:for-each select="document($job)/job/universe/monitor/step">
    <xsl:text/>Step <xsl:value-of select="@id"/>: (<xsl:text/>
    <xsl:variable name="test-requests"
                  select="document($monitor)/monitor/site/test/request
                          [@id=current()/request/@id]"/>
    <xsl:value-of select="count($test-requests)"/>
    <xsl:text>) </xsl:text>
    <xsl:variable name="sum-test-requests"
                  select="sum($test-requests/data/@value)"/>
    <xsl:value-of select="$sum-test-requests"/>
    <xsl:text>  </xsl:text>
    <xsl:choose>
      <xsl:when test="$sum-test-requests > @timeout">
        <xsl:text>TIMEOUT</xsl:text>
      </xsl:when>
      <xsl:otherwise>NO TIMEOUT</xsl:otherwise>
    </xsl:choose>
    <xsl:text/> [<xsl:value-of select="@timeout"/><xsl:text>]
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>rem Done!


-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords