Skip to content

SoftXPath is small cross browser JavaScript library for parsing complex XML documents on client side using powerful Xpath expressions. Focus on building effective Xpath expressions instead of wasting time on browser compatibility issues.

Notifications You must be signed in to change notification settings

softxml/SoftXpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

SoftXpath

SoftXPath is small cross browser JavaScript library for parsing complex XML documents on client side using powerful Xpath expressions. Focus on building effective Xpath expressions instead of wasting time on browser compatibility issues.


Browsers supported:

supported web browsers

SoftXPath Tutorials & Examples

Include SoftXPath library in your file:

<script language="javascript"  src="js/SoftXPath.js"></script>

Create new instance of SoftXpath

var myXpathObj = new SoftXpath();
Method Description Example Result
load expects valid path to xml file as parameter and loads given file
myXpathObj.load("flash_vs_page.xml")
true/false
loadXML expects valid xml string as parameter and loads given string
myXpathObj.loadXML("<root><item color='blue'>demo1</item><item color='red'>demo2</item><item color='blue'>demo3</item></root>")
true/false
registerNamespace expects prefix/URI pair and sets user defined namespaces
default is none - myXpathObj.registerNamespace("","")
myXpathObj.registerNamespace("xsl","http://www.softxml.com");
myXpathObj.loadXML("<xsl:root xmlns:xsl='http://www.softxml.com'> <xsl:item color='blue'>demo1</xsl:item><xsl:item color='red'>demo2</xsl:item><xsl:item color='blue'>demo3</xsl:item></xsl:root>");
//After defining namespace you don't need to change your xpath expression
var results = myXpathObj.selectNodes("//item[@color='blue']");
selectNodes expects valid xpath expression and returns array of objects
var results = myXpathObj.selectNodes("//item[@color='blue']")
Array of Objects
Check array length to see if there are matched nodes:
if(results.length==0){
	alert("No records found");
}
else
{
 //loop over matched nodes
for(var i=0;i<results.length;i++) { //Print current node's text alert(results[i].text); //Print current node's parent element name //me points to current node itself alert(re[i].me.parentNode.nodeName); //Print current node's name alert(re[i].me.nodeName); } }

Examples

SoftXpath load method Our XML file called "flash_vs_page.xml" and it's structure shown below:

	<?xml version="1.0"?>
		<xsl:categories xmlns:xsl="http://www.softxml.com">
			<xsl:category name="CLeasing">
				<xsl:language name="en">
					<xsl:page name="aboutSixt.asp" value="video_41.swf"/>
					<xsl:page name="aboutUs.asp" value="video_42.swf"/>
					<xsl:page name="advantage.asp" value="video_43.swf"/>
					<xsl:page name="club.asp" value="video_44.swf"/>
					<xsl:page name="Cservice.asp" value="video_45.swf"/>
					<xsl:page name="infoSafety.asp" value="video_46.swf"/>
					<xsl:page name="LetterFinal.asp" value="video_47.swf"/>
					<xsl:page name="orderInfo.asp" value="video_48.swf"/>
					<xsl:page name="pressRelease.asp" value="video_49.swf"/>
					<xsl:page name="Resultview.asp" value="video_50.swf"/>
					<xsl:page name="roadTests.asp" value="video_51.swf"/>
					<xsl:page name="SearchAdvanceB.asp" value="video_52.swf"/>
					<xsl:page name="SearchQuickB.asp" value="video_53.swf"/>
					<xsl:page name="SearchResultB.asp" value="video_54.swf"/>
					<xsl:page name="service.asp" value="video_55.swf"/>
					<xsl:page name="SI_jobs.asp" value="video_56.swf"/>
					<xsl:page name="SI_orderFix.asp" value="video_57.swf"/>
					<xsl:page name="SI_writeus.asp" value="video_58.swf"/>
					<xsl:page name="SupplementsB.asp" value="video_59.swf"/>
					<xsl:page name="ResultView.asp" value="video_60.swf"/>
					<xsl:page name="branches.asp" value="video_61.swf"/>
					<xsl:page name="login.asp" value="video_62.swf"/>
					<xsl:page name="makelogin.asp" value="video_63.swf"/>
					<xsl:page name="personal.asp" value="video_64.swf"/>
				</xsl:language>
				<xsl:language name="he">
					<xsl:page name="aboutSixt.asp" value="new_kopel_key_120x150.swf"/>
					<xsl:page name="aboutUs.asp" value="new_kopel_key_120x151.swf"/>
					<xsl:page name="advantage.asp" value="new_kopel_key_120x152.swf"/>
					<xsl:page name="club.asp" value="new_kopel_key_120x153.swf"/>
					<xsl:page name="Cservice.asp" value="new_kopel_key_120x154.swf"/>
					<xsl:page name="infoSafety.asp" value="new_kopel_key_120x155.swf"/>
					<xsl:page name="LetterFinal.asp" value="new_kopel_key_120x156.swf"/>
					<xsl:page name="orderInfo.asp" value="new_kopel_key_120x157.swf"/>
					<xsl:page name="pressRelease.asp" value="new_kopel_key_120x158.swf"/>
					<xsl:page name="accident.asp" value="new_kopel_key_120x159.swf"/>
					<xsl:page name="Resultview.asp" value="new_kopel_key_120x160.swf"/>
					<xsl:page name="roadTests.asp" value="new_kopel_key_120x161.swf"/>
					<xsl:page name="SearchAdvanceB.asp" value="new_kopel_key_120x162.swf"/>
					<xsl:page name="SearchQuickB.asp" value="new_kopel_key_120x163.swf"/>
					<xsl:page name="SearchQuickBNew.asp" value="new_kopel_key_120x164.swf"/>
					<xsl:page name="icar.asp" value="new_kopel_key_120x165.swf"/>
					<xsl:page name="SearchResultB.asp" value="new_kopel_key_120x166.swf"/>
					<xsl:page name="service.asp" value="new_kopel_key_120x167.swf"/>
					<xsl:page name="SI_jobs.asp" value="new_kopel_key_120x168.swf"/>
					<xsl:page name="SI_orderFix.asp" value="new_kopel_key_120x169.swf"/>
					<xsl:page name="SI_writeus.asp" value="new_kopel_key_120x170.swf"/>
					<xsl:page name="SupplementsB.asp" value="new_kopel_key_120x171.swf"/>
					<xsl:page name="branches.asp" value="new_kopel_key_120x172.swf"/>
					<xsl:page name="login.asp" value="new_kopel_key_120x173.swf"/>
					<xsl:page name="makelogin.asp" value="new_kopel_key_120x174.swf"/>
					<xsl:page name="personal.asp" value="new_kopel_key_120x175.swf"/>
				</xsl:language>
			</xsl:category>
		</xsl:categories>

JavaScript Code

	var myXpathObj = new SoftXpath();
	myXpathObj.registerNamespace("xsl","http://www.softxml.com");
	if(myXpathObj.load("flash_vs_page.xml")){
		var results = myXpathObj.selectNodes("//page[./parent::language[@name='he'] and ./@name='aboutSixt.asp']/@value");
		if(results.length==0){
			alert("No records found!");
		}
		else{
			for(var i=0;i<results.length;i++){
				alert(results[i].text);
			}
		}	
	}
	else{
		alert("Loading xml file failed!");
	}

SoftXpath loadXML method Our XML string shown below:

	<?xml version="1.0"?>
	<catalog>
	   <product description="Cardigan Sweater" product_image="cardigan.jpg">
	      <catalog_item gender="Men's">
	         <item_number>QWZ5671</item_number>
	         <price>39.95</price>
	         <size description="Medium">
	            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
	            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
	         </size>
	         <size description="Large">
	            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
	            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
	         </size>
	      </catalog_item>
	      <catalog_item gender="Women's">
	         <item_number>RRX9856</item_number>
	         <price>42.50</price>
	         <size description="Small">
	            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
	            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
	            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
	         </size>
	         <size description="Medium">
	            <color_swatch image="red_cardigan.jpg">Red</color_swatch>
	            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
	            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
	            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
	         </size>
	         <size description="Large">
	            <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
	            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
	         </size>
	         <size description="Extra Large">
	            <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
	            <color_swatch image="black_cardigan.jpg">Black</color_swatch>
	         </size>
	      </catalog_item>
	   </product>
	</catalog>

JavaScript Code

	var myXpathObj = new SoftXpath();
	myXpathObj.registerNamespace('','');
	if(myXpathObj.loadXML(xmlstring)){
		var results = myXpathObj.selectNodes("//color_swatch[./ancestor::catalog_item/child::item_number[text()='QWZ5671'] and ./parent::size[@description='Medium']]");
		if(results.length==0){
			alert("No records found!");
		}
		else{
			for(var i=0;i<results.length;i++){
				alert(results[i].text);
			}
		}
	}
	else{
		alert("Loading xml string failed!");
	}

Online Demo

SoftXpath Online Demo

About

SoftXPath is small cross browser JavaScript library for parsing complex XML documents on client side using powerful Xpath expressions. Focus on building effective Xpath expressions instead of wasting time on browser compatibility issues.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published