views:

151

answers:

4

The xsl contains function uses string values... but how can the value of the searchedforstring be assigned dynamically? It should be possible to assign the value of a text entry field in an html form to a variable and use this as the value of the searched for string in the xsl document.

The following script snippet gets the value of the text entered and assigns it to the global variable strName*. The problem is to use the value of strName in place of the searched for string.

<script language="text/javascript">
var strName;
function nameDetails()
{var strName = getElementById("txtField1").value;}
</script>
A: 

Your question isn't entirely clear, but I think this might suffice.

You can assign the value to some node in your XML document, and then pick that up from your XSL (maybe assigning it into an XSL Variable or something). You can then use the XSL variable in your XSL.

Unless I have misunderstood, in which case, can you elaborate more.

James Wiseman
Hi, I've created an example to make things clearer. I've included details as a word doc... which seemed easiest that you can retrieve from eyle.org/XSLcontainsExample.doc and includes a comprehensive view. Alternatively I posted the XML file to eyle.org/books1.xml and the XSL file is at eyle.org/contains1books.xsl ... hope you can help –
1anthony1
http://www.eyle.org/XSLcontainsExample.doc http://www.eyle.org/books1.xml http://www.eyle.org/contains1books.xsl
1anthony1
A: 

The content of you question doesn't seem to bear any relation to XSL. My guess is you are looking for a way to pass a parameter to the processing of an XSL. My answer in this question may well provide some clues. IF you can clarify your question with more detail then perhaps a more specific example can be formulated.

AnthonyWJones
A: 

As anthony pointed out, I think you want to use a parameter with your stylesheet so that you can pass in the value to match on.

Using your stylesheet, I added the param "author", which you can pass in any value.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:param name="author"/>
  <xsl:output method="html"
     omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <html>
       <head>
       <title>books example</title>
       </head>

    <body>
       <xsl:apply-templates select="//book"/>
    </body>
    </html>
  </xsl:template>


  <xsl:template match="book">
       <xsl:if test="contains(author, $author)">

       <DIV>
         <I><xsl:value-of select="title"/></I> by
         <J><xsl:value-of select="author"/></J> genre
         <B><xsl:value-of select="book_genre"/></B>

       </DIV>
     </xsl:if>
  </xsl:template>
  </xsl:stylesheet>

Not sure how you are invoking your stylesheets, but passing a param would be something like:

xslProc.addParameter("author", strName);
Mads Hansen
A: 

Thanks Mads - very helpful but still have problem,

xslProc.addParameter("author", strName); works just fine. I wasn't sure how to use this with an html document but managed to find and adapt an example (http://newmedia.purchase.edu/~Jeanine/interfaces/parmexample.doc).

The bad news is that my file works fine in Internet Explorer but doesn't work in Firefox. (http://www.eyle.org/containsBooks1.html) Try inputting Mike and then Richard into the textfield... in IE. The example doc includes code for cross browser support but it doesn't work with my file.

My files are HTML: (saved as www.eyle.containsBooks1.html) The XML file is: (saved as www.eyle.books2.xml) The XSL file is: (saved as www.eyle.contains3books.xsl)

Hope you can help with cross browser Thanks

1anthony1