XSLT, finding pcdata

PCDATA

1. Finding PCDATA prior to a given element

1.

Finding PCDATA prior to a given element

Jeni Tennison

>  I need to remove some PCDATA prior to a specified element. I am
>  rendering my XML to HTML. In the main section I want all the
>  information shown. In the table of references that gets generated at
>  the end of the document, I want to strip certain elements and the
>  PCDATA that appears directly before that element.
>
>  Currently, I have templates that ignore the elements that I'm not
>  interested, but I'm not sure how to get to the PCDATA before this
>  element.

You can match text() whose immediately following sibling is a foo element using the match pattern:

   text()[following-sibling::node()[1][self::foo]]

Put that in an empty template, and it will ignore all those text elements:

<xsl:template match="text()[following-sibling::node()[1][self::foo]]"
               mode="ToC" />

If you have more elements whose immediately preceding text you want to ignore, then you can add them to the predicate:

   text()[following-sibling::node()[1]
           [self::foo or self::bar or self::baz]]

So you can get rid of text for all these elements using just one template.

You say:

>  I have created a simple example of what I want to do below. In this
>  example it would be simple to just look at the elements that I do
>  want, but in my real project there are too many elements that I do
>  want. I really only want to add a couple of templates to handle
>  ignoring the element and its preceding PCDATA.

If you want to select all elements aside from a chosen few, you can use the same kind of syntax, using the self:: axis. For example, the following selects all elements that aren't foo, bar or baz elements.

   <xsl:apply-templates
     select="*[not(self::foo or self::bar or self::baz)]"
     mode="ToC" />

This is slightly better (I think) than applying templates to all the elements and having empty templates that match those you don't want because it narrows down the nodes that the templates are applied to early on.