XSLT How to check if XML Node exists?

I have an 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 the 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 do it like this: if a node exists, display the table with Info nodes, and if not, display SOME TEXT.

I have tried

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

      

and

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

      

But it doesn't work.

I want like this:

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

      

(condition: if there is a node inside the XML, I want to display the second table in table 1)

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

      

How can i do this?

+3


source to share


2 answers


This outputs Yep

if <AddInfo>

exists as an immediate child <Record>

, 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 to for-each

, you must allow the second pattern to match each one <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 can also avoid choose

and use two independent conditions if

:

  <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 restrict it to immediate children, use it .//AddInfo

instead.

Consider the following style sheet:

<?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 is no <AddInfo>

node in <Record>

, and

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

      

otherwise.

You can solve this by not using if

and 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>

      

+11


source


To check if a node exists in xml this XSLT code works



<xsl:choose>
                <xsl:when test="XMLNodeName">
                  <Cell ss:Index="2" ss:StyleID="s110">
                    <Data ss:Type="String">
                      <xsl:value-of select="NodeOne"/>
                    </Data>
                  </Cell>
                </xsl:when>
                <xsl:otherwise>
                  <Cell`enter code here` ss:Index="2" ss:StyleID="s110">
                    <Data ss:Type="String">
                      <xsl:value-of select="NodeTwo"/>
                    </Data>
                  </Cell>
                </xsl:otherwise>
 </xsl:choose>

      

0


source







All Articles