Xslt and xpath: match preceding comments?

This seems to work: comment()/following-sibling::*1/self::item It looks for immediately following siblings of comments which are also item elements. I don't know a better way to express the ::*1/self::item part, which is ugly; note that if it were written ::item1 then it would also find item s not immediately proceded by a comment.

This seems to work: //comment()/following-sibling::*1/self::item It looks for immediately following siblings of comments which are also elements. I don't know a better way to express the ::*1/self::item part, which is ugly; note that if it were written ::item1 then it would also find s not immediately proceded by a comment.

Certainly an edge case but the original poster first needs to clarify whether an 'item' element preceded by a processing instruction preceded by a comment is an element he wants to select. – Martin Honnen Apr 10 '10 at 12:35 @Kevin: Thanks, Kevin. This X-Path is simple enough for me to grasp, and it works perfectly :) @Martin: Luckily I am the master of the Xml input, so I'm sure the won't be any processing instructions.

Thanks anyway for the useful hint. – miasbeck Apr 10 '10 at 13:52 Martin Honnen's comments are correct. – Dimitre Novatchev Apr 10 '10 at 16:09.

The currently selected solution: //comment()/following-sibling::*1/self::item doesn't work in the case where there is a procesing instruction (or a whole group of processing instructions) between the comment and the element -- as noticed in a comment by Martin Honnen. The solution below doesn't have such a problem. The following XPath expression selects only elements nodes that are either immediately preceded by a comment node, or are immediately preceded by a white-space-only text node, which is immediately preceded by a comment node: (//comment() /following-sibling::node() 1 self::text() and not(normalize-space()) /following-sibling::node() 1 self::item ) | (//comment() /following-sibling::node() 1 self::item ) Here is a complete test: We use this XML document: When the following transformation is applied on the above XML document: the wanted, correct result is produced.

Good and complete answer. +1 – VonC Apr 10 '10 at 16:04.

As mentioned in this thread, introducing a test () like: preceding-sibling::comment() would only tests whether the node has a preceding sibling that's a comment. If you want to know, of the preceding siblings that are elements or comments, whether the nearest one is a comment, you could try: (preceding-sibling::*|preceding-sibling::comment())1self::comment() # WRONG BUT: that won't work, because though "1" means first in the backwards direction for preceding-sibling, it doesn't mean that for a parenthesized expression - it means first in document order You can try: (preceding-sibling::*|preceding-sibling::comment())last()self::comment() or preceding-sibling::node()self::*|self::comment()1self::comment() For instance: would only display: foo another foo when typing: C:\Prog\xslt\preceding-sibling_comment> java -cp ..\saxonhe9-2-0-6j\saxon9he. Jar net.sf.saxon.

Transform -s:test. Xml -xsl:t. Xslt -o:res.

Xml with: test. Xml: your file displayed in your question t. Xslt: the xslt file above res.

Xml: the resulting transformed file Edit: since it doesn't take into account processing instructions, I left that answer as Community Wiki.

Thanks for your thorough answer VonC! I tried the "if" construct and it works for me. I'd rather use the expression from Kevin though, as it is something I can understand (and hopefully remember).

– miasbeck Apr 10 '10 at 13:54 This doesn't work in the situation described by Martin Honnen. – Dimitre Novatchev Apr 10 '10 at 15:47 1 @Dimitre: +1 on your answer, and I leave mine as Community Wiki. – VonC Apr 10 '10 at 16:07 Thanks, you were very close, too.

– Dimitre Novatchev Apr 10 '10 at 16:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions