XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
TL WSubject: search in XQuery
Author: TL W
Date: 02 Apr 2006 09:21 AM
Hi,

In my document "index.xml":

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="index.xsd">
<sitename>XML/XSL Photo Application</sitename>
<css>style.css</css>
<nav>
<link>
<name>abstract</name>
<url>index.xml</url>
</link>
<link>
<name>architecture</name>
<url>architecture.xml</url>
</link>
<link>
<name>nature</name>
<url>nature.xml</url>
</link>
<link>
<name>food</name>
<url>food.xml</url>
</link>

<sectionname>abstract</sectionname> <!-- The abstract gallery -->
<thumbs>
<!-- Image One -->
<thumb>
<thumbsnavhref>abstract-brokenheart-main.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Broken Heart</thumbsnavtitle>
<thumbssrc>abstract-brokenheart.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Broken Heart</captiontitle>
<indent>0</indent>
</thumb>
<!-- Image Two -->
<thumb>
<thumbsnavhref>notsureyet.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Lonely Footsteps</thumbsnavtitle>
<thumbssrc>abstract-lonelyfootsteps.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Lonely Footsteps</captiontitle>
<indent>1</indent>
</thumb>
<!-- Image Three -->
<thumb>
<thumbsnavhref>notsureyet.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Mouse</thumbsnavtitle>
<thumbssrc>abstract-mouse.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Mouse</captiontitle>
<indent>1</indent>
</thumb>
<!-- Image Four -->
<thumb>
<thumbsnavhref>notsureyet.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Pure White</thumbsnavtitle>
<thumbssrc>abstract-purewhite.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Pure White</captiontitle>
<indent>0</indent>
</thumb>
<!-- Image Five -->
<thumb>
<thumbsnavhref>notsureyet.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Falling Droplet</thumbsnavtitle>
<thumbssrc>abstract-fallingdroplet.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Falling Droplet</captiontitle>
<indent>1</indent>
</thumb>
<!-- Image Six -->
<thumb>
<thumbsnavhref>notsureyet.xml</thumbsnavhref>
<thumbsnavclass>galleryimage</thumbsnavclass>
<thumbsnavtitle>Beads</thumbsnavtitle>
<thumbssrc>abstract-beads.jpg</thumbssrc>
<thumbswidth>140</thumbswidth>
<thumbsheight>80</thumbsheight>
<captiontitle>Beads</captiontitle>
<indent>1</indent>
</thumb>
</thumbs>

</nav>
</site>



I wanted to find all caption titles that contain the word "broken", regardless of the level of nesting.

Now I think the below code does the job. But how would I adapt this so the user can enter any search term and it will bring back the caption title if the word is matched in the index.xml file?

<results>

{

for $i in document("index.xml")//title[contains(./captiontitle(),"broken")]

return {$i}

}

</results>

Again thanks for your time.

Postnext
Minollo I.Subject: search in XQuery
Author: Minollo I.
Date: 02 Apr 2006 10:03 PM
You can create an external variable and bind it through the API consistent with the processing engine you are using.

You could change your test into:

declare variable $text external;

<results>
{
for $i in doc("c:\books.xml")//title[contains(captiontitle,$text)]
return $i
}
</results>

(btw, document() doesn't exist in XQuery/XPath, and "captiontitle" is not a function)

In Stylus Studio you can click XQuery > Scenario Properties, choose the "Parameter Values" tab, and enter something like "broken" (don't forget double or single quotes).

If you want to see the code required to do that, make sure you choose DataDirect XQuery or Saxon as your XQuery processor, select XQuery > Generate Java Code and you will see the code you need to bind external variables.

Hope this helps,
Minollo

Postnext
TL WSubject: search in XQuery
Author: TL W
Date: 03 Apr 2006 05:24 AM
Thanks Minollo.

Ive actually now figured out to implement a search function so a user can enter a search term and if you enter something that matches the “thumbsnavtitle” it will bring back the image etc.

Only problem is it doesn’t bring back any result, heres the code:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:url="uri:find-url.org"
exclude-result-prefixes="msxsl">

<xsl:output method="html" indent="yes" encoding="iso-8859-1"/>


<xsl:variable name="xmlpath" select="url:findURL(.)" />
<xsl:variable name="search">
<xsl:if test="contains($xmlpath,'?')">
<xsl:value-of select="substring-after($xmlpath,'?')" />
</xsl:if>
</xsl:variable>
<msxsl:script language="JScript" implements-prefix="url">
function findURL(nodelist)
{
return nodelist.nextNode().url;
}
</msxsl:script>

<xsl:template match="/">

<html>

<link rel="stylesheet" type="text/css">
<xsl:attribute name="href">
<xsl:value-of select="site/css"/>
</xsl:attribute>
</link>

<head>
<title>
<xsl:value-of select="site/sitename"/>
</title>
</head>

<body>
<form name="search" method="get">
Username:
<input type="text" name="searchstring" />
<input type="submit" value="Submit" />
</form>

<xsl:for-each select="site/nav/thumbs/thumb">

<xsl:choose>
<xsl:when test="thumbsnavtitle = $search">

<img>
<xsl:attribute name="height">
<xsl:value-of select="thumbsheight" />
</xsl:attribute>

<xsl:attribute name="width">
<xsl:value-of select="thumbswidth" />
</xsl:attribute>

<xsl:attribute name="alt">
<xsl:value-of select="thumbsnavtitle" />
</xsl:attribute>

<xsl:attribute name="src">
<xsl:value-of select="thumbssrc" />
</xsl:attribute>
</img>
<a>
<xsl:attribute name="href">
<xsl:value-of select="thumbsnavhref" />
</xsl:attribute>

<xsl:value-of select="thumbsnavtitle" />
</a>
</xsl:when>

<xsl:otherwise>

</xsl:otherwise>
</xsl:choose>


</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

We'll I figured it out with a bit of help from a site:
http://rdcpro.com/Members/rdcpro/snippets/xsltquerystrings. But it doesnt work at the moment any idea what I have done wrong? Heres the link to it http://www.contemporarymultimedia.com/XML/site2.xml

After finding something that potentially works I have now read that using scripting inside the XSLT has been deemed a security risk http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/xmlsdk/html/ba3e4d4f-1ee7-4226-a51a-78a1f1b5bd8a.asp

Is there a way to get around this?

Thanks.

Postnext
TL WSubject: search in XQuery
Author: TL W
Date: 03 Apr 2006 05:26 AM
Using XQuery?

Postnext
Minollo I.Subject: search in XQuery
Author: Minollo I.
Date: 03 Apr 2006 08:50 AM
Are you asking an XSLT question? Are you asking how to do that in XQuery? Yes, XSLT purists won't like Java(Script) extensions; and it does seem to be useless to use them in your case.

As we usually write when questions are general language questions, we suggest that you post them on the xsl-list at mulberrytech.com, or on http://www.xquery.com/ for XQuery.

Minollo

Posttop
TL WSubject: search in XQuery
Author: TL W
Date: 03 Apr 2006 12:34 PM
Yes all i want to do is to be able to do this in XQuery but im finding very difficult in finding or getting someone to explain how to this in XQuery.

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

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