[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Xml/xpath/xslt
Adam Chang wrote: > I am currently working on a xml file. What I want to do about it is to > build a query library which can accept some query (like the tag name) > and then return the value of that tag. I hope I can use the same > function in the query library to get the same result even though the xml > file changes (but not dramatically). This is a fairly simple application to build around an XML parser, although it gets more complex if you need to account for the possibility of multiple occurrences of those names, or elements that have more than just text as their content. For code specifics you can read the tutorials and demos for just about any XML parser, but I'll give you the gist of it here: A DOM-based XML parser will read the whole document into a node tree, and then you can use the most common DOM method, getElementsByTagName(), on the document node to get all the element nodes that have a particular name. From there you can look at the data in the child nodes. A SAX-based XML parser will read the document, calling event methods like startElement(), endElement() and characters(), when it encounters certain structures in the markup. You provide your own versions of these methods to do what you need to.. such as in startElement() you would set a flag if the element has a certain name, similarly unsetting the flag in endElement(), and in characters() you append the incoming character data to a buffer if that flag is set. > I don't know if Xpath or XSLT will help me on that because I want to > build the query library in Java and Xpath & XSLT seems mainly working for > web pages. XSLT is for building an XML-like node tree structure, usually based on a similar XML-based tree provided as input, and then, usually, outputting that tree according to some common syntax like XML, HTML, or plain text. XPath defines such trees and how they relate to logical XML structures, and it provides a syntax for identifying nodes in those trees and performing certain functional operations on them. You could certainly use an XSLT processor with a very small stylesheet like <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:param name="n" select="'nonExistentElementName'"/> <xsl:template match="/"> <xsl:value-of select="//*[name()=$n]"/> </xsl:template> </xsl:stylesheet> With this stylesheet, when the element name you're searching for is given via external stylesheet parameter 'n', you'll get in your output the string-value (XPath terminology meaning, in this case, the concatenation of all text descendants) of the first element node that has that name. - Mike ____________________________________________________________________________ mike j. brown | xml/xslt: http://skew.org/xml/ denver/boulder, colorado, usa | resume: http://skew.org/~mike/resume/
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|