Re: [xsl] XPath behaves differently when opened by document()

Subject: Re: [xsl] XPath behaves differently when opened by document()
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Mon, 2 Aug 2010 16:35:12 +0200
If your stylesheet is meant to do some lookup in the secondary source based
on data in the primary source I recommend that you do not try to use
apply-templates for the lookup but switch to call-template, passing in
parameters taken from the primary document.

-W


On 2 August 2010 16:34, Wolfgang Laun <wolfgang.laun@xxxxxxxxx> wrote:
>
> If your stylesheet is meant to do some lookup in the secondary source based
on
> date in the primary source I recommend that you do not try to use
apply-templates
> for the lookup but switch to call-template, passing in parameters taken
from
> the primary document.
>
> -W
>
> On 2 August 2010 16:11, Jack Bush <netbeansfan@xxxxxxxxxxxx> wrote:
>>
>> Hi All,
>>
>> There appears to be 2 undesirable features/effects felt when opening
secondary
>> document using document() function in XSLT 2.0 (Saxon 9.1) on Windows XP.
Let's
>> examine the stylesheet in question as follows:
>>
>> 1 <xsl:param name="FILE_LIST_PARAM" as="xs:string*" required="no"
select="()"/>
>> 2 <xsl:param name="COMPANY_NAME_PARAM"/>
>> 3
>> 4 <xsl:template match="/">
>> 5 <employee-profile>
>> 6 <company_name><xsl:value-of
select="$COMPANY_NAME_PARAM"/></company_name>
>> 7 <xsl:for-each select="document($FILE_LIST_PARAM)/ns:html/ns:body/ns:div">
//
>> secondary source
>> 8 <xsl:for-each
>>
select="/ns:html/ns:body/ns:div[@id='content']/ns:table[@class='sresults']/ns
:tr/ns:td/ns:a">
>>  // main source
>> 9 <employee_name><xsl:value-of
>> select="substring-before(@title,',')"/></employee_name> // main source
>> 10 <employee_address><xsl:value-of select="@href"/></employee_address> //
main
>> source
>> 11 </xsl:for-each> // main source
>> 12   <xsl:apply-templates/> // secondary source
>> 13 </xsl:for-each> // secondary source
>> 14 </employee-profile>
>> 15 </xsl:template>
>> 17
>> 18 <xsl:template match="ns:html/ns:body/ns:div">
>> 19 <financial_status>
>> 20 <xsl:variable name="salary" select="//ns:p[ns:strong='Salary:']"/>
>> 21 <xsl:apply-templates select="$salary"/>
>> 22 <xsl:if test="empty($salary)">
>> 23 <salary>Unknown</salary>
>> 24 </xsl:if>
>> ......
>> 35 </financial_status>
>> 36 </xsl:template>
>> 37
>> 38 <xsl:template match="ns:p[ns:strong='Salary:']">
>> 39 <xsl:for-each select="ns:a">
>> 40 <salary><xsl:value-of select="."/></salary>
>> 41 </xsl:template>
>> ..........
>> 55 <xsl:template match="ns:p"/>
>> 56
>> 57 </xsl:stylesheet>
>>
>> ( i ) document() needs more specific detail XPath statement to lookup the
desire
>> node. Otherwise, the data for every single subnodes will be retrieved
before
>> getting to target node. For instance, line 7 would include irrelevant data
that
>> is part of subnodes along the path of getting to the destination node.
>>
>> ( ii ) On the other hand, yet when providing specific XPath instruction
would
>> appears to query every nodes under the specified branch which is
preventing
>> their individual templates from being run altogether. Again, if line 7 were
to
>> changed from:
>>
>> 7 <xsl:for-each
select="document($FILE_LIST_PARAM)/ns:html/ns:body/ns:div">
>>                                                     to
>> 7 <xsl:for-each
select="document($FILE_LIST_PARAM)/ns:html/ns:body/ns:div/p"> or
>> <xsl:for-each
select="document($FILE_LIST_PARAM)/ns:html/ns:body/ns:div//p">
>>
>>
>> where all the data is located in each <p> node but would no longer execute
line
>> 18 - 55, which was the original working stylesheet used to open the same
>> document as primary source.
>>
>> In addition, I am also having difficulty getting data (switching between
>> documents) from main and secondary documents using nested for-each loop
(line 7
>> - 11). The result is that only the outer loop (7, 12-13; refer to
secondary
>> document) is being taken effect but not the internal loop (8 - 11; refer to
main
>> source).
>>
>> Hope I haven't confused everyone already.
>>
>> Any suggestion would be much appreciated.
>> Thanks in advance,
>> Jack

Current Thread