p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

xslt thread: document() : access to all elements


Message #1 by cmr <mr016393@m...> on Tue, 10 Sep 2002 08:29:48 +0200
> I would like to apply templates to all the elements in a 
> file. The document name is given by a parameter "Doc_nameID".
> 
> I use the function document($Doc_nameID) to point to a file. I can 
> access some elements from the file but the problem is that I 
> don't know 
> how access all the elements.
> 
> How could I define and manipulate a node-set ?

The function document($Doc_nameID) returns a single node, the root of
the document. You can get to the outermost element of the document by
writing:

document($Doc_nameID)/*

The node-set containing all elements in the document is:

document($Doc_nameID)//*

If the outermost element is <doc>, and the second level elements are
<row> elements, then you can access all the <row> elements using

document($Doc_nameID)/doc/row

or

document($Doc_nameID)/*/row

or

document($Doc_nameID)//row

I hope this helps.

Michael Kay
Software AG
home: Michael.H.Kay@n...
work: Michael.Kay@s... 

> 
> 
> I have tried:
> 
> 1.- <xsl:for-each select="document($docEscName)">

That will just process the root node.

> 2.- Copying the value in a variable
> 
>    <xsl:variable name="resp">
>      <xsl:copy-of select="document($docEscName)"/>
>    </xsl:variable>

That will create a copy of the document, which you don't need.
> 
> The following code access only the first element in document $docName
> 
> 
> <!-- ****************************************************** 
> --> <xsl:template name="ExtractInfoDoc" >
> <!-- ****************************************************** 
> --> <xsl:param  name="Doc_nameID" />
> 
>  <xsl:variable name="docName" select="concat($Doc_nameID,'.xml')"/>
> 
> 
>  <xsl:for-each select="document($docName)"> 
>    <br /> <br />  <b> 
>     <xsl:value-of select="document($docName)//relation/@type"/></b>
>    <br /> 
>     <xsl:value-of select="document($docName)//relation/role/@name"/> 
> 
> 
>     <xsl:variable name="ID_object" >
>       <xsl:value-of 
>         select="normalize-space(document($docName)/*/object/@id)"/>
>     </xsl:variable>
> 
> 
>      <xsl:call-template name="LookMoreInfo">
>        <xsl:with-param name="object_nameID" select="$ID_object"/>
>      </xsl:call-template>
>   <br />
> 
> 
> 
>    <xsl:value-of  select="document($docName)//attribute/@name"/> 
>   <br />
> 
>  </xsl:for-each>
> </xsl:template>
> 
> 
> 
> 
> Note:
> LookMoreInfo: look for information in a specific file 
>                using keys and the document function too.
>                It works!.
> 
> ---
> In developing the .NET Framework Microsoft has 
> taken XML to heart, and with ASP.NET representing 
> the cutting edge of Windows based web development, 
> Professional ASP.NET XML with C# has been published 
> to help web developers to appreciate how these 
> technologies can work together to provide 
> functional, powerful applications. 
> http://www.wrox.com/acon11.asp?ISBN=1861007345
> 


  Return to Index