xsl-list
[Top] [All Lists]

Re: [xsl] Test processing instruction as first preceding sibling

2008-12-09 15:34:15
At 2008-12-09 13:58 -0200, Alexandre Moraes wrote:
Hi guys,

I have a doubt about testing preceding-sibling processing-instruction.
        I have this xml:

       <root>
           <child1>
                   child 1
           </child1>
           <?proc?>
           <child2>
                  child 2
          </child2>
     </root>

           And I need to know if child2 has the first
preceding-sibling as a processing-instruction. In this case it should
return true.

But it isn't true. There is a text node between the processing instruction and <child2>.

            The xml can come like this too:

     <root>
           <child1>
                   child 1
           </child1>
           <child2>
                  child 2
          </child2>
      </root>
        In this case it should return false

and like this:

      <root>
          <?proc?>
           <child1>
                   child 1
           </child1>
           <child2>
                  child 2
          </child2>
      </root>

and here false too.

I tryed something like this

preceding-sibling::*[1]/name()|preceding-sibling()[1]/name = 'proc'

but it didn´t work.

Can anyone help me??

The following:

         preceding-sibling::node()[not(self::text())][1]/
                     self::processing-instruction('proc')

says:

  "Get the preceding sibling nodes of any kind, looking only at the
   non-text nodes, then looking only at the first of those, checking
   that node is a processing instruction with the name 'proc'."

I hope this helps.

. . . . . . . . . . Ken

T:\>type alexandre.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="//child2">
    <xsl:choose>
      <xsl:when test="preceding-sibling::node()[not(self::text())][1]/
                      self::processing-instruction('proc')">Yes!</xsl:when>
      <xsl:otherwise>No!</xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\>type alexandre1.xml
       <root>
           <child1>
                   child 1
           </child1>
           <?proc?>
           <child2>
                  child 2
          </child2>
     </root>

T:\>xslt alexandre1.xml alexandre.xsl con
Yes!
T:\>type alexandre2.xml
     <root>
           <child1>
                   child 1
           </child1>
           <child2>
                  child 2
          </child2>
      </root>

T:\>xslt alexandre2.xml alexandre.xsl con
No!
T:\>type alexandre3.xml
      <root>
          <?proc?>
           <child1>
                   child 1
           </child1>
           <child2>
                  child 2
          </child2>
      </root>

T:\>xslt alexandre3.xml alexandre.xsl con
No!
T:\>


--
Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes:
:  Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--