Unknown error in XPath (using variables)
This is a multi-part message in MIME format.
--------------030404060205060307050301
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Hi there!
I am trying to evaluate an XPath expression using variables, like this:
"$foo/Street" -- where $foo is a variable with an Address element,
containing a Street element:
<Address>
<ID>Fakturering</ID>
<Street>Holsteinsgade</Street>
<HouseNumber>63</HouseNumber>
<CityName>K?benhavn ?.</CityName>
<PostalZone>2100</PostalZone>
<Country>
<Code>DK</Code>
</Country>
</Address>
While evaluating the simple "$foo" expression works fine, the
"$foo/Street" expression throws an exception with the message "Unknown
error in XPath".
Any help, hints or references to documentation is appreciated. Thank you
for your time. I hope to be able to help you out some day.
The attached source code and XML file illustrates this problem if you
like to try it out your self. I used a JDK 1.5.0_08.
--------------030404060205060307050301
Content-Type: text/x-java;
name="Main.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="Main.java"
package dk.hallwyl.tim.xpath;
import java.io.File;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathVariableResolver;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class Main implements XPathVariableResolver {
private String expression = "$foo/Street";
private String xml = "invoice.xml";
private XPath xpath;
private DocumentBuilder documentBuilder;
private NodeList variable;
public Main() {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
System.out.println("Using XPath-factory:\n " + xPathFactory);
xpath = xPathFactory.newXPath();
System.out.println("Using XPath:\n " + xpath);
xpath.setXPathVariableResolver(this);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
System.out.println("Using DocumentBuilderFactory:\n " + documentBuilderFactory);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
System.out.println("Using DocumentBuilder:\n " + documentBuilder);
System.out.println("Setting up variable data:");
Document document = documentBuilder.parse( new File(xml) );
System.out.println(" - Parsed XML-file:\n " + xml);
System.out.println(" - Evaluating expression (for variable data):\n /Invoice/BuyerParty/Address");
variable = (NodeList) xpath.evaluate("/Invoice/BuyerParty/Address", document, XPathConstants.NODESET);
System.out.println(" - Variable data is set.");
System.out.println("--------------------------------------------------------------------------------");
System.out.println(" Setup is complete");
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Evaluating expression:\n " + expression);
NodeList result = (NodeList) xpath.evaluate(expression, (Object) null, XPathConstants.NODESET);
if (result != null) {
System.out.println("Expression returned " + result.getLength() + " nodes:");
for (int i = 0; i < result.getLength(); i++) {
System.out.print(nodeToString(result.item(i)));
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public Object resolveVariable(QName variableName) {
System.out.println("Resolving variable " + variableName.getLocalPart());
return variable;
}
public static void main(String[] args) {
Main main = new Main();
}
public static String nodeToString(Node node) { return nodeToString(node, 0); }
public static String nodeToString(Node node, int indent) {
if (node == null)
return "null";
String xml = "";
String tab = "\n";
for (int i = 0; i < indent; i++)
tab += " ";
if (node instanceof Text) {
Text text = (Text) node;
if (text.isElementContentWhitespace()) {
} else {
xml += tab + " " + node.getNodeValue();
}
}
else {
xml += tab;
//xml += "<" + node.getNodeName() + " xmlns:" + node.getPrefix() + "=\"" + node.getNamespaceURI() + "\">";
xml += "<" + node.getNodeName() + attributesToString(node) + ">";
if (node.getNodeValue() != null)
xml += tab + " " + node.getNodeValue();
else {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
xml += nodeToString(children.item(i), indent + 2);
}
xml += tab;
xml += "</" + node.getNodeName() + ">";
}
return xml;
}
private static String attributesToString(Node node) {
NamedNodeMap map = node.getAttributes();
if (map == null) return null;
Node attr;
String attrs = "";
for (int i = 0; i < map.getLength(); i++) {
attr = map.item(i);
attrs += " " + attr.getLocalName() + "=\"" + attr.getNodeValue() + "\"";
}
return attrs;
}
}
--------------030404060205060307050301
Content-Type: text/xml;
name="invoice.xml"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline;
filename="invoice.xml"
<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
<ID>2296</ID>
<IssueDate>2004-10-13</IssueDate>
<TypeCode>PIE</TypeCode>
<main:InvoiceCurrencyCode>DKK</main:InvoiceCurrencyCode>
<BuyersReferenceID>5798000416642</BuyersReferenceID>
<ReferencedOrder>
<BuyersOrderID>M23-453</BuyersOrderID>
<SellersOrderID/>
<IssueDate>2004-10-01</IssueDate>
</ReferencedOrder>
<BuyerParty>
<ID schemeID="CVR">12312312</ID>
<PartyName>
<Name>IT- og Telestyrelsen</Name>
</PartyName>
<Address>
<ID>Fakturering</ID>
<Street>Holsteinsgade</Street>
<HouseNumber>63</HouseNumber>
<CityName>K??benhavn ??.</CityName>
<PostalZone>2100</PostalZone>
<Country>
<Code>DK</Code>
</Country>
</Address>
<BuyerContact>
<ID>rak@itst.dk</ID>
</BuyerContact>
</BuyerParty>
<SellerParty>
<ID schemeID="CVR">22222222</ID>
<PartyName>
<Name>Company name A/S</Name>
</PartyName>
<Address>
<ID>Betaling</ID>
<Street>Jernbanegade</Street>
<HouseNumber>875</HouseNumber>
<CityName>Roskilde</CityName>
<PostalZone>4000</PostalZone>
</Address>
<PartyTaxScheme>
<CompanyTaxID schemeID="CVR">22222222</CompanyTaxID>
</PartyTaxScheme>
</SellerParty>
<TaxTotal>
<TaxTypeCode>VAT</TaxTypeCode>
<TaxAmounts>
<TaxableAmount currencyID="DKK">5300.00</TaxableAmount>
<TaxAmount currencyID="DKK">1325.00</TaxAmount>
</TaxAmounts>
<CategoryTotal>
<RateCategoryCodeID>VAT</RateCategoryCodeID>
<RatePercentNumeric>25</RatePercentNumeric>
<TaxAmounts>
<TaxableAmount currencyID="DKK">5300.00</TaxableAmount>
<TaxAmount currencyID="DKK">1325.00</TaxAmount>
</TaxAmounts>
</CategoryTotal>
</TaxTotal>
<LegalTotals>
<LineExtensionTotalAmount currencyID="DKK">5300.00</LineExtensionTotalAmount>
<ToBePaidTotalAmount currencyID="DKK">6625</ToBePaidTotalAmount>
</LegalTotals>
<InvoiceLine>
<ID>1</ID>
<InvoicedQuantity unitCode="stk." unitCodeListAgencyID="n/a">100</InvoicedQuantity>
<LineExtensionAmount currencyID="DKK">300</LineExtensionAmount>
<Item>
<ID>4523</ID>
<Description>Kuglepenne med logo</Description>
</Item>
<BasePrice>
<PriceAmount currencyID="DKK">3</PriceAmount>
</BasePrice>
</InvoiceLine>
<InvoiceLine>
<ID>2</ID>
<InvoicedQuantity unitCode="kasse" unitCodeListAgencyID="n/a">50</InvoicedQuantity>
<LineExtensionAmount currencyID="DKK">5000</LineExtensionAmount>
<Item>
<ID>4533</ID>
<Description>Brevpapir med logo - Kasse med 1000 ark.</Description>
</Item>
<BasePrice>
<PriceAmount currencyID="DKK">100</PriceAmount>
</BasePrice>
</InvoiceLine>
</Invoice>
--------------030404060205060307050301--