Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3399866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:49:23+00:00 2026-05-18T04:49:23+00:00

I have an XSLT 1.0 stylesheet that needs to either output the value of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:49:24+00:00Added an answer on May 18, 2026 at 4:49 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xslt stylesheet that needs to call a C# XSLT extension function
I have an XSLT stylesheet that transforms an XML file to JSON format and
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I have got an XSLT that looks like this: <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=xml
I have this XSLT 2.0: <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output indent=yes/> <xsl:strip-space elements=*/> <xsl:template match=node()|@*>
I have an XSLT stylesheet that processes an XML document to produce HTML. I've
Lets say I have a xslt stylesheet like the following: <?xml version=1.0 encoding=utf-8?> <xsl:stylesheet
I have an XML EDITED: <?xml version=1.0 encoding=UTF-8?> <?xml-stylesheet type=text/xsl href=xsl.xslt?> <CATALOG> <CD> </CD>
I have an XSLT transform issue: style=width:{Data/PercentSpaceUsed}%; And the value of Data/PercentSpaceUsed is integer
I've generated the following XSLT file, and have created a Form that will post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.