user2726752
user2726752

Reputation: 45

XSL. Return only the first Non Empty Element

XML:

<?xml version="1.0" encoding="utf-8"?>
    <NewDataSet>
      <inc_incident>
        <inc_assessmentvitalsigns>
          <DateVitalSignsTaken>2013-10-06T20:23:36</DateVitalSignsTaken>
          <SBP>10</SBP>
          <DBP>20</DBP>
          <inc_cardiacrhythm>
            <CardiacRhythm></CardiacRhythm>
          </inc_cardiacrhythm>
        </inc_assessmentvitalsigns>
        <inc_assessmentvitalsigns>
          <DateVitalSignsTaken>2013-10-06T20:24:36</DateVitalSignsTaken>
          <SBP>0</SBP>
          <DBP>0</DBP>
          <inc_cardiacrhythm>
            <CardiacRhythm>Asystole</CardiacRhythm>
          </inc_cardiacrhythm>
        </inc_assessmentvitalsigns>
        <inc_assessmentvitalsigns>
          <DateVitalSignsTaken>2013-10-06T20:25:36</DateVitalSignsTaken>
          <SBP>0</SBP>
          <DBP>0</DBP>
          <inc_cardiacrhythm>
            <CardiacRhythm>Sinus Tachycardia</CardiacRhythm>
          </inc_cardiacrhythm>
        </inc_assessmentvitalsigns>
      </inc_incident>
    </NewDataSet>

XSL: (Only snippet)

<fo:table-row>
 <fo:block>
  <xsl:for-each select="inc_assessmentvitalsigns">
   <xsl:if test="inc_cardiacrhythm/cardiacRhythm !=''">
    <xsl:for-each select="inc_cardiacrhythm/CardiacRhythm">
       <fo:inline font-family="Verdana, Arial, sans-serif" font-size="9pt">
        <xsl:apply-templates />
       </fo:inline>
  </xsl:for-each>
   </xsl:if>
    </xsl:for-each>
 </fo:block>
</fo:table-row>
   .......
<fo:table-row>
</fo:table-row>

I'm using APACHE FOP to generate a PDF document with tables and rows.

Question is How to return ONLY the first non empty element for CardiacRhythm? If I use

xsl:value-of
 select

then the next row of the table is truncated. I can't figure this out. Thanks for any help.

The result of the xsl:

AsystoleSinus Tachycardia

Upvotes: 3

Views: 1910

Answers (3)

har07
har07

Reputation: 89285

You can use the following XPath selector to get only the first non-empty <cardiacRhythm> element :

(inc_cardiacrhythm/cardiacRhythm[normalize-space()])[1]

Note that element containing white-spaces only also considered empty elements which won't be selected by the above XPath.

Upvotes: 0

cis
cis

Reputation: 1377

To get the first child element CardiacRhythm which has some charater data in it I would prefer an XPath expression like CardiacRhythm[text()[string-length(.) gt 0]][1] Which means: Go to all childs named CardiacRhythm which have at least one text node whose string-length is greater than zero. Than select the first from these.

Upvotes: 1

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

Would this do:

<xsl:for-each select="inc_assessmentvitalsigns[inc_cardiacrhythm/cardiacRhythm !=''][1]">
    <fo:inline font-family="Verdana, Arial, sans-serif" font-size="9pt">
        <xsl:apply-templates select="inc_cardiacrhythm/cardiacRhythm" />
    </fo:inline>
</xsl:for-each>

Upvotes: 0

Related Questions