Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL. Return only the first Non Empty Element

Tags:

xslt

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

like image 818
user2726752 Avatar asked Dec 09 '25 17:12

user2726752


1 Answers

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.

like image 93
cis Avatar answered Dec 12 '25 20:12

cis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!