user269597
user269597

Reputation:

Output a string if a given element does not exist in XSLT

I have an XSLT 1.0 stylesheet that needs to either output the value of a specific element if that element exists, or output the string "NULL" if it doesn't. How can I accomplish this?

Update

The document I'm working with looks something like this:

<kanjidic2>
    <header>
        <file_version>4</file_version>
        <database_version>2010-325</database_version>
        <date_of_creation>2010-11-20</date_of_creation>
    </header>
    <character>
        <literal>亜</literal>
        <codepoint>
            <cp_value cp_type="ucs">4e9c</cp_value>
            <cp_value cp_type="jis208">16-01</cp_value>
        </codepoint>
    </character>
    <character>
        <literal>𢛳</literal>
        <codepoint>
            <cp_value cp_type="ucs">226F3</cp_value>
            <cp_value cp_type="jis213">2-12-48</cp_value>
        </codepoint>
    </character>
    <!-- Plus a few thousand more <character>s -->
</kanjidic2>

I'm writing an XSLT stylesheet to transform the above into a series of MySQL queries. Initially I wanted to output NULL if the character did not have a jis208 codepoint associated with it (hence my initial question), producing a query like this:

INSERT INTO `kanji` (`literal`, `ucs`, `jis208`, ...) VALUES ('亜', '4e9c', '16-01', ...);
INSERT INTO `kanji` (`literal`, `ucs`, `jis208`, ...) VALUES ('𢛳, '226F3', NULL, ...);

I've since realised that I could make the XSLT simpler and produce a shorter query instead:

INSERT INTO `kanji` (`literal`, `ucs`, `jis208`, ...) VALUES ('亜', '4e9c', '16-01', ...);
INSERT INTO `kanji` (`literal`, `ucs`, `jis213`, ...) VALUES ('𢛳', '226F3', '2-12-48', ...);

The solution

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <output method="text" encoding="UTF-8"/>

    <template match="/">
        <apply-templates select="kanjidic2/character"/>
    </template>

    <template match="/kanjidic2/character">
        <text>INSERT INTO `kanji` (`literal`</text>
        <apply-templates select="codepoint/cp_value" mode="first"/>
        <text>) VALUES ('</text>
        <value-of select="literal"/>
        <apply-templates select="codepoint/cp_value" mode="second"/>
        <text>);&#10;</text>
    </template>

    <template match="/kanjidic2/character/codepoint/cp_value" mode="first">
        <text>, `</text>
        <value-of select="@cp_type"/>
        <text>`</text>
    </template>
    <template match="/kanjidic2/character/codepoint/cp_value" mode="second">
        <text>, '</text>
        <value-of select="."/>
        <text>'</text>
    </template>
</stylesheet>

I will mark Dimitre Novatchev's answer as the correct one because it is the most succinct solution to my initial question.

Thanks for your help.

Upvotes: 1

Views: 1861

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:param name="pDefault">NULL</xsl:param>
 <xsl:variable name="vDefault" select=
  "document('')/*/xsl:param[@name='pDefault']"/>

 <xsl:variable name="vDoc" select="/"/>

 <xsl:template match="/">
     <xsl:value-of select=
      "concat(//myelement[@myattribute='avalue'],
              $vDefault[not($vDoc/*/myelement[@myattribute='avalue'])]
              )"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<mydoc>
    <myelement myattribute="avalue">Some text</myelement>
    <myelement myattribute="anothervalue">Some more text</myelement>
</mydoc>

produces the wanted, correct result:

Some text

When applied on this XML document:

<mydoc>
    <myelement myattribute="anothervalue">Some more text</myelement>
</mydoc>

again the wanted, correct result is produced:

NULL

Do note:

No conditional instruction (<xsl:if> or <xsl:choose>/<xsl:when>/<xsl:otherwise>) is used at all.

Upvotes: 0

Erica
Erica

Reputation: 2251

Assuming that there will only ever be zero or one matches in a given document, but that they could be at any location within that document, the following will probably do what you're after:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="//myelement[@myattribute = 'avalue']"><xsl:value-of select="//myelement[@myattribute = 'avalue']"/></xsl:when>
            <xsl:otherwise>NULL</xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

If they're always going to be in a specific point in the document, you can make the above more efficient by changing the "//" in the path to "myelement" to the proper path.

Upvotes: 1

Oded
Oded

Reputation: 499132

You can use count with xsl:if to output different values:

<xsl:if test="count(myelement[@myattribute = 'avalue']) = 0">
NULL
</xsl:if>
<xsl:if test="count(myelement[@myattribute = 'avalue']) &gt; 0">
  <xsl:value-of select="myelement" />
</xsl:if>

Upvotes: 0

Related Questions