Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

XSLT Detect processing-instruction in current-group returned by for-each-group

I want to test a group returned from a "for-each-group" to see if it consists of a single node which is a processing-instruction.

Currently in the choose statement, the first when and second when work. The second when tests for a blank node.

The third when attempts to test for a processing-instruction, but it does not work.

I need some syntax that will detect a processing instruction.

<xsl:for-each-group select="node()" group-adjacent="...">
  <xsl:choose>
    <xsl:when test="current-grouping-key()">
      <xsl:apply-templates select="current-group()" mode="..."/>
    </xsl:when>
    <xsl:when test="count(current-group()) eq 1 and current-group()/self::text() and not(normalize-space(current-group()))">
     <!-- skip blank nodes -->
    </xsl:when>
    <xsl:when test="count(current-group()) eq 1 and current-group()/self::processing-instruction()">
      do special stuff...
    </xsl:when>
    <xsl:otherwise>
         do stuff...
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each-group>

Open in new window

The input might be:
<block>
  
        <section>
          ...
        </section>
        <section>
          ...
        </section>

    <?foo bar1?>
  
        <section>
          ...
        </section>
        <section>
          ...
        </section>

    <?foo bar2?>

        <section>
          ...
        </section>
</block>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

The group-adjacent="x:boolean_function(.)" is a boolean function and returns true or false if it's on a list or not.

I've tried all sorts of combinations of processing-instruction() and haven't come up with anything that works. Not sure if I need the test for count = 1. The debugger shows it on the processing instruction, and jumping to the otherwise clause no matter what when statement I put in there. (Altova XMLSpy. Is there a way I can get that debugger to tell me something useful?)
OK, "its on a list or not" I assume means that the element name is on a list?
so, basically that means that all elements on a list are grouped together
and everything in between (not on a list) are grouped together
if the processing instruction are not on that list (and I assume because otherwise they would be in the first when clause)
they would group together with the whitespace nodes before and after

basically my assumption is right
count(current-group()) eq 1 and current-group()/self::processing-instruction()
will not be true() because the current-group is a sequence of more than one item
Avatar of deleyd

ASKER

Oh and forgot to mention, if I throw in a <value-of select="current-group()"/> I do get the name from the processing instruction it's on.
Avatar of deleyd

ASKER

OK let me try that I'll get back (I'm not at the office right now).
note that in an adjacent I would do something like this
group-adjacent="if(x:boolean_function(.)) then(generate-id()) else('not-listed')"

it has the advantage that each elements that renders the boolean true is in its seperate group
and all the others are grouped too
you can then in the fallback group do an apply-templates to a different mode and take action based on the node type
it is a bit more convenient (less when clauses) and better readable code
just suggesting
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

So I'm actually trying to match on a sequence containing a processing instruction mixed with whitespace nodes? Those darn whitespace nodes. Maybe there's a way I can filter them out beforehand.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

Oh I see it's returning the content Plus the whitespace, which probably does not have any spaces in it.
Avatar of deleyd

ASKER

Thank you very much for the help!