xpath - Name des Attributes herausfinden

hallbast

Grünschnabel
Hallo,
ich möchte aus folgender xml mit xpath den Namen des attributes bei title herausfinden: (Ergebnis sollte: lang, country und village sein). Geht das? Ich verzweifle gerade....
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title country="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title village="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <year>2003</year>
  <price>49.99</price>
</book>

</bookstore>

Vielen Dank
Steffen
 
Hallo,

mit dem XPath Ausdruck:
Code:
//title/@*
selektiert man alle Attribute aller Title-Elemente

Mit:
Code:
//book[@category='CHILDREN']/title/@country
selektieren wir das country-Attribut des Titel Elements des Buchs aus der Category "CHILDREN".


Gruß Tom
 
Hallo Tom,
ist leider glaube ich nicht das was ich brauche. Ich will den NAMEN des Attributes, nicht den Wert.
Folgende Variablen würde ich gerne herausbekommen: lang, country, village
Das macht es glaube ich nicht, leider. Oder teste ich falsch?
Habe es mit dem von dir verlinkten Tool im IE (den ich sonst nie brauche) gemacht: http://xpathvisualizer.codeplex.com/ (DANKE dafür :) )
Aber schon mal vielen Dank.
Grüße Steffen
 
Zuletzt bearbeitet:
Hallo,

mit //title/@* werden wie bereits gesagt alle Attribut-Knoten des Titelelements selektiert, also Wert und NAME.
Java:
package de.tutorials;

import java.io.FileReader;

import javax.xml.crypto.NodeSetData;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XPathExample {
	public static void main(String[] args) throws Exception {
		NodeList list = (NodeList)XPathFactory.newInstance().newXPath().evaluate("//title/@*", new InputSource(new FileReader("test.xml")),XPathConstants.NODESET);
		for(int i = 0, len = list.getLength();i<len;i++){
			Node node = list.item(i);
			System.out.println(node.getNodeName() + " " + node.getNodeValue());
		}
	}
}

Test.xml: Das obige XML Dokument.

Ausgabe:
Code:
lang en
country en
village en

Gruß Tom
 
Hi.

Du kannst auch die XPath Funktion name() bzw. local-name() benutzen.

Allerdings mußt du mit einer Schleife über alle Knoten des Knotensets //title/@* iterieren.

Gruß
 
... Allerdings mußt du mit einer Schleife über alle Knoten des Knotensets //title/@* iterieren...
... offensichtlich gar nicht mal unbedingt notwendig. Folgendes Stylesheet auf obiges XML angewandt ...
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />

  <xsl:variable name="attrName1" select="name(//book[1]/title/@*)" />
  <xsl:variable name="attrName2" select="name(//book[2]/title/@*)" />
  <xsl:variable name="attrName3" select="name(//book[3]/title/@*)" />

  <xsl:template match="/">

    <attrNamen>
      <xsl:for-each select="//title/@*">
        <attrName>
          <xsl:value-of select="name(.)" />
        </attrName>
      </xsl:for-each>

      <attrVar1><xsl:value-of select="$attrName1" /></attrVar1>
      <attrVar2><xsl:value-of select="$attrName2" /></attrVar2>
      <attrVar3><xsl:value-of select="$attrName3" /></attrVar3>

    </attrNamen>
  </xsl:template>

</xsl:stylesheet>
... ergibt bei mir unter XMLSpy diesen Output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<attrNamen>
  <attrName>lang</attrName>
  <attrName>country</attrName>
  <attrName>village</attrName>
  <attrVar1>lang</attrVar1>
  <attrVar2>country</attrVar2>
  <attrVar3>village</attrVar3>
</attrNamen>
 
... offensichtlich gar nicht mal unbedingt notwendig.
Naja, auf den allgemeinen Fall von Tausenden von book Elementen sollte man evtl. doch eine Schleife verwenden... :)

Klar kann man von bestimmten, einzelnen Attributen den Namen ausgeben ohne eine Schleife, aber was ist z.B. wenn ein Element mehrere Attribute hat?

Gruß
 
Zurück