Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-09-2012, 06:30 PM   PM User | #1
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 241
Thanks: 51
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
XPath to select all nodes with attributes that contain a sub-string?

Quoting from http://www.w3schools.com/xpath/xpath_syntax.asp:

Quote:
//title[@lang='eng']

Selects all the title elements that have an attribute named lang with a value of 'eng'
But what syntax do I use to select all the title elements that have an attribute named lang with a value that contains the substring 'eng'?
XmisterIS is offline   Reply With Quote
Old 02-10-2012, 06:47 AM   PM User | #2
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,931
Thanks: 6
Thanked 194 Times in 191 Posts
Arbitrator is on a distinguished road
Quote:
Originally Posted by XmisterIS View Post
But what syntax do I use to select all the title elements that have an attribute named lang with a value that contains the substring 'eng'?
You would use the contains function. Example:

document_1.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="document_1.xslt"?>
<document>
	<title>Title 1</title>
	<title lang="eng">Title 2</title>
	<title lang="eng-US">Title 3</title>
	<title lang="eng-Latn-US">Title 4</title>
</document>
document_1.xslt
Code:
<?xml version="1.0" encoding="UTF-8"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<output media-type="application/xhtml+xml"/>
	<template match="/">
		<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<title>Demo Document</title>
				<style>
					* { margin: 0; padding: 0; }
					html { margin: 2em; }
					h1 { font-size: 1em; }
					ol { margin: 0.25em 0 1em; padding-left: 1em; }
				</style>
			</head>
			<body>
				<h1>English Titles</h1>
				<ol>
					<xslt:for-each select="child::document/child::title[contains(attribute::lang, 'eng')]">
					<!-- child::document/child::title[contains(attribute::lang, 'eng')] = document/title[contains(@lang, 'eng')] -->
						<li><xslt:value-of select="self::node()"/></li>
					</xslt:for-each>
				</ol>
			</body>
		</html>
	</template>
</stylesheet>
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 02-10-2012, 07:18 AM   PM User | #3
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,931
Thanks: 6
Thanked 194 Times in 191 Posts
Arbitrator is on a distinguished road
Okay, apparently the code above needs a bit of modification:

Code:
<output media-type="application/xhtml+xml"/>
must be

Code:
<output method="xml" media-type="application/xhtml+xml"/>
for this code to work in Internet Explorer 9 because that browser does not implement section 16 of the XSLT 1.0 spec correctly.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Users who have thanked Arbitrator for this post:
XmisterIS (02-10-2012)
Old 02-10-2012, 09:34 AM   PM User | #4
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 241
Thanks: 51
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
Thanks, perfect
XmisterIS is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.