Subwayboy Posted May 21, 2011 Share Posted May 21, 2011 Hi all,I am working on a small dashboard type set of pages to be used in IE7(and only IE7). I am now stumbling on one piece of Javascript / XSLT(Version 1.0)I have a person name field and an associated select html element tofilter the list of names from the xml file. This filter works fineexcept for one person's name that contains and apostrophe. I havetried using replace() function to add in a ' and escaping with a\ but to no avail. All other name values filter fine, just this onename, and i'm sure there will be other names appearing with an ' in it....The error message states : "Expected token ']' found 'NAME' //Row[Name=O'-->Callaghan<--]Should i change the encoding on the xml file itself? The XML isgenerated from an MS Excel 2007 workbookMany thanksGrahamXML (data.xml) <?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root><Row><Name>Graham</Name></Row><Row><Name>Graham</Name></Row><Row><Name>Graham</Name></Row><Row><Name>Graham</Name></Row><Row><Name>Smith</Name></Row><Row><Name>O'Callaghan</Name></Row></Root> XSL (list.xsl) <?xml version="1.0"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" /><xsl:variable name="Rows" select="//Row" /><xsl:template match="//Root"><xsl:for-each select="$Rows"><p><xsl:value-of select="Name" /></p></xsl:for-each></xsl:template></xsl:stylesheet> HTML / Javascript <html><head><script language="javascript">function LoadXML(xmlname){var xmlDOCxmlDOC = new ActiveXObject("Microsoft.XMLDOM");xmlDOC.async = false;xmlDOC.load(xmlname);return(xmlDOC);}function TransformXML(data, xform){xml=LoadXML(data);xsl=LoadXML(xform);dataFields = xml.transformNode(xsl);document.getElementById("divContent").innerHTML=dataFields;}function FilterXML(sFilter){filterNode = list.XMLDocument.selectSingleNode("xsl:stylesheet/xsl:variable[@name='Rows']")filterNode.setAttribute("select", "//Row[Name='" + sFilter + "']")dataFields = xml.transformNode(list.XMLDocument)document.getElementById("divContent").innerHTML=dataFields;}</script></head><body onLoad="TransformXML('data.xml', 'list.xsl')"><xml id="list" src="list.xsl" /><select id="cboList" onChange="FilterXML(this.value)"><option>Select..</option><option value="Graham">Graham</option><option value="Smith">Smith</option><option value="O'Callaghan">O'Callaghan</option></select><div id="divContent"></div></body></html> Link to comment Share on other sites More sharing options...
Martin Honnen Posted May 21, 2011 Share Posted May 21, 2011 If you construct XPath expressions dynamically then you have to make sure you follow the XPath syntax rules. Assuming your names can contain single quotes but not double quotes it might suffice to change filterNode.setAttribute("select", "//Row[Name='" + sFilter + "']") to filterNode.setAttribute("select", '//Row[Name="' + sFilter + '"]') A much better approach however is to define a parameter with xsl: param in your stylesheet, then use MSXML's API to run the transformation with setting parameters http://msdn.microsoft.com/en-us/library/ms...v=VS.85%29.aspx.Another issue, though not related to your question, is that you have xsl:stylesheet version="2.0" although your target IE and MSXML. MSXML is an XSLT 1.0 processor so consider to use xsl:stylesheet version="1.0" as otherwise the processor runs in forwards compatible mode and will not report errors it would otherwise report. Link to comment Share on other sites More sharing options...
Subwayboy Posted May 23, 2011 Author Share Posted May 23, 2011 Many thanks. I did not realise that single and double quotes could be swapped in this way. All is working fine now. Graham Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.