Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 15th, 2006, 12:18 PM
Authorized User
 
Join Date: Feb 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default DOM Attribute name/values

Using Document Object Model

How do I read the attribute name and value of an element

ie

<Book PUBLISHER="Wrox">

The Beginning XML Book only outlines how to read node values. Which I have no trouble doing but I do not know how to read attribute names/values.

Thanks

Brendon

 
Old February 15th, 2006, 12:26 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There are a number of ways, assuming <Books> as the root element, here are two.
  • Code:
    var oNode = oDom.selectSingleNode("/Books/Book/@PUBLISHER");
    var sPublisher = oNode.nodeValue;
  • Code:
    var oNode = oDom.selectSingleNode("/Books/Book");
    var sPublisher = oNode.getAttribute("PUBLISHER");

--

Joe (Microsoft MVP - XML)
 
Old February 15th, 2006, 06:05 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the attribute name isn't namespaced, then the simplest method to get its value (in DOM) is

getAttribute("PUBLISHER")

applied to the Element representing the Book.

I'm a little surprised that you should have difficulty finding this in the documentation, so perhaps I misunderstood your problem.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help, Combine two like attribute values mikhen7 XSLT 4 July 10th, 2006 02:25 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
how to get values depending on the attribute vidhya XSLT 1 July 8th, 2005 03:55 AM
change values XML with DOM miguel.ossa XML 1 February 6th, 2004 05:12 PM
different values with the same attribute name srini XSLT 0 January 21st, 2004 05:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.