Grafit
Grafit

Reputation: 699

XPath/XSLT/Axes select all siblings including self

It seems logical to me that there would be an easy axis or something else to select the text of all siblings, including self, but I can't seem to find it.

XML:

<panelTabs>
 <field></field>
 <field></field>
</panelTabs>

I'm currently in a <xsl:template match="panelTabs/field>, and I need to be in this template. I want to check whether or not all of the values within every <field> are empty, How do I do this?

Edit:
To be a little more specific. I would like my XSLT to be something like this:

<xsl:template match="panelTabs/field>
 <xsl:if test="allfieldshaventgottext = true">
  <p>All are empty</p>
 </xsl:if>
 <xsl:if test="thereisafieldwithtext = true">
  <p>There is a field with text</p>
 </xsl:if>
</xsl:template>

Instead of an xsl:if, an xsl:when will do

EDIT:
I created a new, more explained question. I'ts here: XPath/XSLT select all siblings including self

Upvotes: 0

Views: 2902

Answers (2)

JLRishe
JLRishe

Reputation: 101748

You can use ../* to select all siblings including the current element (or ../field to specifically select field elements).

So in your case, you could do:

<xsl:template match="panelTabs/field">
 <xsl:if test="not(../field[normalize-space()])">
  <p>All are empty</p>
 </xsl:if>
 <xsl:if test="../field[normalize-space()]">
  <p>There is a field with text</p>
 </xsl:if>
</xsl:template>

Example with some non-blank

Example with all blank

However, it would be more idiomatic to use pattern matching:

<xsl:template match="panelTabs/field">
  <p>All are empty</p>
</xsl:template>

<xsl:template match="panelTabs[field[normalize-space()]]/field" priority="2">
  <p>There is a field with text</p>
</xsl:template>

Example with some non-blank

Example with all blank

If you only want to check once whether all of the fields are blank, you can do this:

<xsl:template match="panelTabs[not(field[normalize-space()])]">
  <p>All are empty</p>
</xsl:template>

<xsl:template match="panelTabs/field">
  <p><xsl:value-of select="." /></p>
</xsl:template>

<xsl:template match="panelTabs/field[not(normalize-space())]" priority="2" />

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167716

If you check not(../field[not(normalize-space())]) then you know there is no field that is empty or simply contains white space.

Upvotes: 0

Related Questions