package testUnit;
![](/Images/OutliningIndicators/None.gif)
import java.io.IOException;
//import java.util.Iterator;
//import java.util.List;
import java.util.Properties;
![](/Images/OutliningIndicators/None.gif)
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
![](/Images/OutliningIndicators/None.gif)
import org.w3c.dom.*;
import org.xml.sax.SAXException;
![](/Images/OutliningIndicators/None.gif)
import net.sf.saxon.dom.*;
//import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.query.*;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class XMLTest
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
query();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* Configuration c = new Configuration(); StaticQueryContext qp = new
* StaticQueryContext(c); XQueryExpression xe = qp.compileQuery("");
* DynamicQueryContext dqc = new DynamicQueryContext(c);
* dqc.setContextNode(new DocumentWrapper(tidyDOM, url, c)); List result =
* xe.evaluate(dqc);
*/
}
![](/Images/OutliningIndicators/InBlock.gif)
@SuppressWarnings("deprecation")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void query()
{
String query = "<ul>\n"
+ "{\n"
+ "for $b in //bib/book\n"
+ "where $b/publisher = \"Addison-Wesley\" and $b/@year > 1992 "
+ "return\n" + "<li>{ data($b/title) }</li>\n" + "}\n"
+ "</ul>";
Document doc = input("test.xml");
Configuration c = new Configuration();
StaticQueryContext qp = new StaticQueryContext(c);
XQueryExpression xe;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
xe = qp.compileQuery(query);
DynamicQueryContext dqc = new DynamicQueryContext(c);
dqc.setContextNode(new DocumentWrapper(doc, null, c));
final Properties props = new Properties();
props.setProperty(OutputKeys.METHOD, "xml");
props.setProperty(OutputKeys.INDENT, "yes");
xe.run(dqc, new StreamResult(System.out), props);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (XPathException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static Document input(String filename)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
![](/Images/OutliningIndicators/InBlock.gif)
Document doc = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
builder = factory.newDocumentBuilder();
doc = builder.parse(filename);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (SAXException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
doc.normalize();
return doc;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void output(Document _doc)
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
transformer = tFactory.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.INDENT, "yes");
properties.setProperty(OutputKeys.ENCODING, "GB2312");
properties.setProperty(OutputKeys.METHOD, "xml");
properties.setProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperties(properties);
DOMSource source = new DOMSource(_doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (TransformerConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (TransformerException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}test.xml内容:
<bib>
<book year="1994">
<title>TCP/IP Illustrated</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="1992">
<title>Advanced Programming in the Unix environment</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="2000">
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
</book>
</bib>输出:
<?xml version="1.0" encoding="UTF-8"?>
<ul>
<li>TCP/IP Illustrated</li>
</ul>程序来源于
成功运行第一个XQuery表达式
修改了一处:1992后面加了一个空格