[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to "query" into a different xml?

  • From: "Thomas B. Passin" <tpassin@h...>
  • To: xml-dev@l...
  • Date: Wed, 18 Oct 2000 12:17:42 -0400

xsl querystring
José Manuel Beas asks a significant question about xslt
(reproduced after this response) -

So as as I know, there are two ways to do this.

1)  Use the Saxon xslt processor so you can use its
"evaluate()" extension function to evaluate your path
statement as a node set specification (see Michael Kay's
book "XSLT Programmer's Reference", p 649).  Or,

2) Write a stylesheet to create a stylesheet that does what
you want, then run the second stylesheet.

I think that xslt SHOULD add an evaluate function, and right
away, too!

Here's a working ( I tried it and got the result you say you
want) version of your styleshseet, using the Saxon
"evaluate()" approach:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:saxon="http://icl.com/saxon">
<!--====== Note the saxon namespace! =========-->

<!--====== This part is unchanged ========-->
<xsl:template match="part">
  <H1><xsl:value-of select="@name"/></H1>
  <xsl:variable name="OBJECTNAME" select="class"/>
  <xsl:variable name="QUERYSTRING" select="query"/>
  <xsl:variable name="FILENAME"
select="document('master.xml')//object[@name =
$OBJECTNAME]/@href"/>
  objectname = <xsl:value-of select="$OBJECTNAME"/><BR/>
  filename = <xsl:value-of select="$FILENAME"/><BR/>
  query = <xsl:value-of select="$QUERYSTRING"/><BR/>
<!--===============================-->

<!--==== use for-each to set the current node to the foreign
document ===-->
 <xsl:for-each select="document($FILENAME)">

<!--==== Build the query string and evaluate it ======-->
 <xsl:variable name="path"
select="saxon:evaluate(concat('//',$QUERYSTRING))"/>
  <PRE>

<!--=== The evaluated path contains your data =====-->
  <xsl:value-of select="$path"/>
  </PRE>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Enjoy!

Tom Passin

>
> We have an xml file with a catalogue of parts, but their
descriptions
> are kept in a different xml file (this file depends on the
class of
> the part).
>
> These is the file:
>
> "parts.xml"
> ------------
> <parts>
>   <part name="BigSquare">
>     <class>square</class>
>     <query>square[@id = '001']/color</query>
>   </part>
>   <part name="SmallCylinder">
>     <class>cylinder</class>
>     <query>cylinder[@id = '001']/color</query>
>   </part>
> </parts>
> -----------
>
> I am transforming it into HTML with an XSLT by matching
the "part"
> template.
>
> "process.xslt"
> -----------
> <xsl:template match="part">
>   <H1><xsl:value-of select="@name"/></H1>
>   <xsl:variable name="OBJECTNAME" select="class"/>
>   <xsl:variable name="QUERYSTRING" select="query"/>
>   <xsl:variable name="FILENAME"
select="document('master.xml')//object
> [@name = $OBJECTNAME]/@href"/>
>   objectname = <xsl:value-of select="$OBJECTNAME"/><BR/>
>   filename = <xsl:value-of select="$FILENAME"/><BR/>
>   query = <xsl:value-of select="$QUERYSTRING"/><BR/>
>   <PRE>
>   <xsl:value-of
select="document($FILENAME)//$QUERYSTRING"/>
>   </PRE>
> </xsl:template>
> -----------
>
> What I am doing is to look for the filename corresponding
to each
> part "class" (into "master.xml") and then apply the
"query" to that
> xml file.
>
> But it doesn't work. I get the following error:
>
> oracle.xml.parser.v2.XSLException: XSL-1013: Error in
> expression: 'document($FILENAME)//$QUERYSTRING'.
>
>
> The rest of the files used are:
>
> "master.xml"
> ------------
> <master>
>   <objects>
>     <object name="square" href="squares.xml"/>
>     <object name="cylinder" href="cylinders.xml"/>
>   </objects>
> </master>
> ------------
> "squares.xml"
> ------------
> <squares>
>   <square id="001">
>     <color>Black</color>
>     <size>big</size>
>   </square>
>   <square id="002">
>     <color>Blue</color>
>     <size>small</size>
>   </square>
> </squares>
> -----------
>
> It's curious, but when I replace
>   <xsl:value-of
select="document($FILENAME)//$QUERYSTRING"/>
> by (for example)
>   <xsl:value-of select="document($FILENAME)//square[@id
> = '001']/color"/>
> I get the following:
> -----
> <PRE>
>   Black
> </PRE>
> -----
>
> Which is EXACTLY what I am trying to obtain.
>
> Any ideas???
>



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.