Project

Profile

Help

get number lines of a query

Added by Anonymous over 19 years ago

Legacy ID: #2856654 Legacy Poster: pfc_caw (pfc_caw)

Hi everyone! I'm doing queries to a DOM document that represents a XHTML page. How can i get the number line of the document when a XQuery be false? Example: i want to know the number lines of the elements IMG that dont have ALT atribute (//img[not(@alt)]) Id like to insert an JPG (IMG element) each time an XQuery be false, too. (like above) How can i do this? A lot of thanks! PD: Sorry because of my bad english


Replies (6)

Please register to reply

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2857739 Legacy Poster: Michael Kay (mhkay)

You can count the number of nodes in the result of a path expression using the count function, for example count(//img[not(@alt)]). Inserting an element for all img elements that have no @alt attribute is done in XSLT like this: <xsl:template match=""> <xsl:copy> <xsl:copy-of select="@"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="img[not(@alt)]"> <xsl:copy> <xsl:copy-of select="@"/> <JPG>....</JPG> <xsl:apply-templates/> </xsl:copy> In XQuery the equivalent is a recursive function something like this: declare function my:insertJPG($e as element()) { element {node-name($e)} { $e/@, if (not ($e/@alt)) then element {JPG} {...} else (), for $child in $e/* return my:insertJPG($child) } } Not tested.

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2858368 Legacy Poster: pfc_caw (pfc_caw)

Thanks for your answer. Xquery: if(condition=false) then "return node" else () How can i return the node? And then, in Saxon, how can i get the nodes, and insert a IMG element before them in the DOM document I have been done the query? The nodes Xquery returns will be the same object as the nodes in the DOM document? Do you understand me? Im doing my graduation project and i am blocked at this :(

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2858378 Legacy Poster: pfc_caw (pfc_caw)

Would XQuery be like this? For example: for $b in //img[@alt] return {$b}

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2858399 Legacy Poster: Michael Kay (mhkay)

This list isn't really the right place to ask basic questions about the XQuery language. Have you read a tutorial such as Jonathan Robie's at http://www.datadirect.com/news/whatsnew/xquerybook/index.ssp ? A better place for questions about the language is the list at xquery.com. You can return a node simply by selecting it. For example, the query //item will return all the item elements. XQuery doesn't allow you to modify the source document. Instead (just like XSLT) it works by creating a modified copy of the source document. This means you need to copy everything that you don't want to change, and at the appropriate points add the things you want to add. Michael Kay

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2858445 Legacy Poster: pfc_caw (pfc_caw)

I havent read it but im going to do it. I dont want you are annoyed about my questions. A question about saxon. if the query producing a sequence of items (SequenceIterator), can i do this: org.w3c.DOM.Node nodedom = ?; Node nodequery = (Node)iter.next(); if (nodedom == nodequery){ // do something } Sorry and thank you.

RE: get number lines of a query - Added by Anonymous over 19 years ago

Legacy ID: #2858485 Legacy Poster: Michael Kay (mhkay)

If the Source that you supply as input to Saxon is a DOMSource, then the NodeInfo objects that are returned by a SequenceIterator will be instances of VirtualNode, which is essentially a wrapper for the DOM org.w3.dom.Node object - you can get access to the DOM nodes by calling getUnderlyingNode() on the VirtualNode objects. If the Source that you supply is a StreamSource or SAXSource, then Saxon will construct its own tree representation. In this case the NodeInfo objects returned will be instances of the class AbstractNode. This class implements the DOM Node interface, so you can use DOM methods to access the information - however, updating methods will fail with an UnsupportedOperationException, because Saxon's tree structure is read-only. So the answer depends on how you are constructing the source tree, and on which APIs you are using to access the result. Saxon works with the DOM, but not completely happily. There are too many differences between the DOM model and the XPath/XQuery data model (and too many differences between different DOM levels and different DOM implementations). Unless you are using the DOM for some other reason, I would suggest using Saxon's native tree model in preference. (I'm entirely happy to take Saxon-specific questions on this forum - but it's more useful to other readers if you start a new question on a new thread). Michael Kay

    (1-6/6)

    Please register to reply