XPath - replace function funktioniert nicht

guni

Bekanntes Mitglied
Hallo,

habe ein XML-Dokument, das ca. so aussieht:
[XML]<BLOCK>
<LINE VALUE="5,50"/>
<LINE VALUE="5,50"/>
</BLOCK>[/XML]
jetzt will ich eine Funktion schreiben, die die alle Line-Values eines Blocks summiert.
Auch wenn ich in Dom natürlich mit einer einfachen Schleife alle Zeilen durchlaufen könnte, habe ich mich für einen XPath-Ansatz entschieden (gefiel mir irgendwie besser).

meine Funktion sieht jetzt also so aus:
Java:
public static Double sumAttribute(Element e, String attribute) throws XPathExpressionException 
	{
		System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
		XPathExpression exp = xpath.compile("LINE/sum(*/number(replace(@"+attribute+",',','.')))");
		NodeList result = (NodeList)exp.evaluate(e, XPathConstants.NODESET);
		return (result.getLength() == 0) ? new Double(0) : new Double(result.item(0).getNodeValue());
}

da ich immer einen Fehler erhalte, habe ich meinen XPath mal ein bisschen vereinfacht
Java:
XPathExpression exp = xpath.compile("LINE/@VALUE"); // funktioniert
Java:
XPathExpression exp = xpath.compile("LINE/replace(@VALUE,',','.')"); // funktioniert nicht mehr

für zweiteren Ansatz bekomme ich schon folgenden Fehler:
Java:
Exception in thread "main" net.sf.saxon.trans.XPathException: Extension function required class org.w3c.dom.NodeList; supplied value of class net.sf.saxon.value.StringValue could not be converted
	at net.sf.saxon.dom.DOMObjectModel.convertXPathValueToObject(DOMObjectModel.java:354)
	at net.sf.saxon.dom.DOMObjectModel.access$000(DOMObjectModel.java:39)
	at net.sf.saxon.dom.DOMObjectModel$6.convert(DOMObjectModel.java:163)
	at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:352)
	at com.eurofunk.sag.parsers.test2.XmlLib.sumAttribute(XmlLib.java:192)
	at com.eurofunk.sag.parsers.test3.main.Main.main(Main.java:30)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
	at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:357)
	at com.eurofunk.sag.parsers.test2.XmlLib.sumAttribute(XmlLib.java:192)
	at com.eurofunk.sag.parsers.test3.main.Main.main(Main.java:30)
Caused by: net.sf.saxon.trans.XPathException: Extension function required class org.w3c.dom.NodeList; supplied value of class net.sf.saxon.value.StringValue could not be converted
	at net.sf.saxon.dom.DOMObjectModel.convertXPathValueToObject(DOMObjectModel.java:354)
	at net.sf.saxon.dom.DOMObjectModel.access$000(DOMObjectModel.java:39)
	at net.sf.saxon.dom.DOMObjectModel$6.convert(DOMObjectModel.java:163)
	at net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:352)
	... 2 more
Ich verstehe nicht ganz wieso - der XPath ist gültig!
Könnt ihr mir da weiterhelfen?!

mfg, guni
 

Atze

Top Contributor
hm, ich versteh den nicht, sieht so kompliziert aus :)

warum nicht so?:

Java:
    XPathExpression expr = xpath.compile("//BLOCK/LINE");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    double sum = 0;
    for (int i = 0; i < nodes.getLength(); i++) {
    	String attValue = nodes.item(i).getAttributes().item(0).getTextContent();
    	attValue = attValue.replaceAll(",", "\\.");
        sum += new Double(attValue); 
    }
 

guni

Bekanntes Mitglied
ja klar. so gehts auch.
theoretisch brauch ich gar keinen XPath weil ich ja sowieso mein DOM-Dokument auch durchlaufen könnte.
Wollte aber trotzdem wissen warum die Summierung über XPath in Java nicht funktionert ...
 

Atze

Top Contributor
funktionieren tuts sicherlich, wurde bestimmt schon irgendwo mehrfach eingesetzt. so weit hab ich mich damit auch nicht befasst
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Kirby.exe Probleme mit XML XPATH Select XML & JSON 3
B XPath soll XML liefern XML & JSON 7
O XPATH Problem - Anfänger XML & JSON 4
M dom4j unberechtigte Invalid XPath expression XML & JSON 12
W XPath schreiben XML & JSON 1
P HTML XPath XML & JSON 1
S Ersatz für die veraltete Klasse XPath XML & JSON 1
C XPath: Alle Kinder in einer Schleife auslesen XML & JSON 2
P Xpath zugriff auf Attribute XML & JSON 3
P "XPath is deprecated" XML eclipse XML & JSON 3
G HtmlUnit XPath XML & JSON 5
D XPath-Query XML & JSON 2
I XPath Namensräume und auslesen einzelner Knoten XML & JSON 3
AMStyles XPATH Befehl SVG Java XML & JSON 2
G xpath: in Kindelement zwei Attribut-Werte auslesen XML & JSON 2
G xpath: Inhalt eines Kindelements auslesen XML & JSON 2
H ChildNode via XPath ansprechen XML & JSON 2
G JDOM - aus Children-Liste ein Child direkt ansprechen ohne XPath? XML & JSON 9
S Attribute von Elementen auslesen mit XPath XML & JSON 2
T Xpath & JDOM Element Pfad ausgeben XML & JSON 2
W XPath + Inhalt aus <td> XML & JSON 2
R XPath - frage zur Adressierung XML & JSON 3
reibi XPath XML & JSON 14
M xPath liefert leeres Nodeset XML & JSON 2
M Probleme mit XPath bei Java XML & JSON 5
W Element mit XPath @id ermitteln wenn nur ein teil der id bekannt ist XML & JSON 2
F XPath-Problem mit DOM4J XML & JSON 8
T XPath Problem: finden einer Node nach Attributswert XML & JSON 2
O XPATH gesucht XML & JSON 6
G XPATH-Frage XML & JSON 2
L Element manipulieren in einem DOMResult per XPath XML & JSON 10
P XPath und Namespaces XML & JSON 3
E XPATH-Ausdruck mit not XML & JSON 4
E Wieso liefert dieser XPATH-Ausdruck nicht die richtige Anzahl Treffer? XML & JSON 8
M XPath Ausdruck validieren XML & JSON 2
N XPath Adressierung XML & JSON 7
F XPath frage XML & JSON 4
B Optionaler XPath pfad XML & JSON 2
B XPath frage XML & JSON 2
T Frage zu XQuery (XQJ) / XPath XML & JSON 2
P XPath Problem XML & JSON 2
S Problem mit XPath XML & JSON 4
T Mit XPATH finden und dann änder XML & JSON 2
T EXSD auslesen mit XPath und JDOM XML & JSON 8
G XPath gesucht für Tag mit konkretem Content XML & JSON 2
A XPath Problem XML & JSON 2
C XPath in JDOM klappt nicht XML & JSON 2
T Prbolem XPath XML & JSON 2
X Xpath, alle Element die mit "user" beginnen auswäh XML & JSON 2
loadbrain XPath Problem XML & JSON 2
M brauche große XML-Datei für XPath-Tests XML & JSON 4
M JDOM und XPath, zu Element zugehörigen XPath-Ausdruck XML & JSON 5
F JDOM und XPath - Problem mit Namespace ohne Prefix XML & JSON 5
F hilfe bei xpath-ausdruck XML & JSON 2
K java + xpath -> performanceproblem XML & JSON 9
H Xpath kindelemente auslesen XML & JSON 2
S Xml zurück in xpath wandeln XML & JSON 8
G Frage zu XPath XML & JSON 2
flashfactor Filtern mittels XPATH XML & JSON 4
M XSL/XPath - Nur ersten Wert mit selben Attribut XML & JSON 4
M XPath Problem im Zusammenhang mit document() XML & JSON 2
M Frag zu xsl:when beziehungsweise XPath XML & JSON 3
F JDom und XPath XML & JSON 12
C xpath funktioniert nicht XML & JSON 5
M XPATH und RSS (Problem namespaces) XML & JSON 7
P nochmal XPath :-) XML & JSON 22
P XPath . XML & JSON 2
T Rekursiver Verzeichniss Baum in XML & XPath XML & JSON 4
clemson xpath mit jdom XML & JSON 2
Wildcard xpath Parser XML & JSON 8
R XLM Datei lesen Ändern (kompliziertes Search and Replace) und Speichern XML & JSON 12
M POI HWPF - copy doc and replace text XML & JSON 2
R Jco 3.0 Function und XML XML & JSON 3

Ähnliche Java Themen

Neue Themen


Oben