Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts

    XPath to select all nodes with attributes that contain a sub-string?

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

    //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'?

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,305
    Thanks
    28
    Thanked 276 Times in 270 Posts
    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>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,305
    Thanks
    28
    Thanked 276 Times in 270 Posts
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    XmisterIS (02-10-2012)

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    Thanks, perfect


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •