Html – XSLT How to check if XML Node exists

htmlxmlxslt

I have XML file:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
  <Records>
    <Record>
     <AddInfo>
      <Info>
      </Info>
     </AddInfo>
    </Record>
  </Records>
</Data>

and XSL file:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Dane">
    <html>
      <link rel="stylesheet" type="text/css" href="report.css"></link>
      <body>
        <h2>Table1</h2>
        <table border="1" cellspacing="0">
          <tr>
            <th>XXX</th>
          </tr>
          <xsl:for-each select="Records/Record">
            <tr>
              <td>
                <xsl:value-of select="XXX"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
        <h2>SecondTable</h2>
        <table border="1" cellspacing="0">
          <tr>
            <th>YYY</th>
            <th>ZZZ</th>
          </tr>
          <xsl:for-each select="Records/Record/AddInfo/Info">
            <tr>
              <td>
                <xsl:value-of select="YYY"/>
              </td>
              <td>
                <xsl:value-of select="ZZZ"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

And I want to make it like this: if node exists, display table with "Info" nodes, and if not, display SOME TEXT.

I've been trying

<xsl:if test="following-sibling::AddInfo">
</xsl:if>

and

<xsl:if test="AddInfo">
</xsl:if>

But it is not working.

I want it like this:

Table1
---------------------
|     |      |      |

(condition: if inside XML will be node, I want to display second table, under Table1)

SecondTable
-------------
|     |     |

How I can do this?

Best Answer

This outputs Yep if <AddInfo> exists as immediate child of <Record>, and Nope otherwise:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:for-each select="Records/Record">
      <xsl:choose>
        <xsl:when test="AddInfo">Yep</xsl:when>
        <xsl:otherwise>Nope</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Note that you don't need for-each, you should let a second template match each <Record>:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <xsl:choose>
      <xsl:when test="AddInfo">Yep</xsl:when>
      <xsl:otherwise>Nope</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

You could also avoid choose and use two independent if conditions:

  <xsl:template match="Data/Records/Record">
    <xsl:if test="AddInfo">Yep</xsl:if>
    <xsl:if test="not(AddInfo)">Nope</xsl:if>
  </xsl:template>

If you don't want to limit it to immediate children, use .//AddInfo instead.

Consider the following stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:apply-templates select="Records/Record"/>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <table class="one"></table>
    <xsl:if test="AddInfo">
      <table class="two"></table>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

It outputs

<table class="one"></table>

if there's no <AddInfo> node in <Record>, and

<table class="one"></table>
<table class="two"></table>

otherwise.

You can solve this with neither using if nor choose. XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
  <AddInfo>
    <Info>This is ignored</Info>
  </AddInfo>
  <Records>
    <Record>
        <AddInfo>
          <Info>One,</Info>
          <Info>Two,</Info>
          <Info>Three</Info>
        </AddInfo>
    </Record>
    <Record>
      <Info>Ignored as well</Info>
    </Record>
    <Record>
      <Nested>
        <AddInfo>
          <Info>So is this</Info>
        </AddInfo>
      </Nested>
    </Record>
  </Records>
</Data>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <root>
      <xsl:apply-templates select="Records/Record"/>
    </root>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <xsl:copy>
      <table id="one"></table>
      <xsl:apply-templates select="AddInfo"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Data/Records/Record/AddInfo">
    <table id="two">
      <xsl:apply-templates select="Info"/>
    </table>
  </xsl:template>

  <xsl:template match="Data/Records/Record/AddInfo/Info">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Output:

<root>
  <Record>
    <table id="one" />
    <table id="two">One,Two,Three</table>
  </Record>
  <Record>
    <table id="one" />
  </Record>
  <Record>
    <table id="one" />
  </Record>
</root>