473,770 Members | 3,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Newbie] Output something using XSLT even if an element is missing in some documents?

Hi All,

I am some information from INSPEC database records in XML to build a
relational database of my own. I am currently trying to extract information
by doing an XSLT transform of the XML files into a tab-separated text file
that I want to import into the database. I have run into the following
problem: in some documents there are missing elements, for instance the
volume and issue number of an article is not there (i.e. it is defined in
the DTD, but the element that contains these values happen not to be
included in some documents). This causes a problem because it messes up my
columns as all the rest of the elements I am extracting move left and thus
are placed in wrong columns.

My question is: is there some way in XSLT output a value like "empty" or
"N/A" when some elements are missing?

I use Xalan to do the transformations , and Altova XML spy / Stylevison to
create the XLSTs. Please find a excerpt from my XLST file below; it is the
"vol", "ino" and "voliss" elements that are sometimes empty.

Thank you for any help!

- John

<xsl:for-each select="bibliog ">
<xsl:for-each select="jrefg">
<xsl:for-each select="jrog">
<xsl:for-each select="jt">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="ajt">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="vid">
<xsl:for-each select="vol">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="ino">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="voliss" >
<xsl:apply-templates/>
</xsl:for-each>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="pgn">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
</xsl:for-each>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="norefs" >
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="ti">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
<xsl:for-each select="abs">
<xsl:apply-templates/>
</xsl:for-each>
<xsl:text</xsl:text>
</xsl:for-each>


Sep 27 '08 #1
6 3729
John Larson wrote:
Hi All,

I am some information from INSPEC database records in XML to build a
relational database of my own. I am currently trying to extract information
by doing an XSLT transform of the XML files into a tab-separated text file
that I want to import into the database. I have run into the following
problem: in some documents there are missing elements, for instance the
volume and issue number of an article is not there (i.e. it is defined in
the DTD, but the element that contains these values happen not to be
included in some documents). This causes a problem because it messes up my
columns as all the rest of the elements I am extracting move left and thus
are placed in wrong columns.

My question is: is there some way in XSLT output a value like "empty" or
"N/A" when some elements are missing?
For your inspiration: here an example stylesheet which is properly transforming XML documents into tab-separated text files: http://tinyurl.com/3zf2bo

Hope this helps, Hermann
Sep 27 '08 #2
"Hermann Peifer" <pe****@gmx.net wrote in message
news:48******** ******@gmx.net. ..
For your inspiration: here an example stylesheet which is properly
transforming XML documents into tab-separated text files:
http://tinyurl.com/3zf2bo

Hope this helps, Hermann
Thanks Herman! I have now tried to construct a new XSLT based on your
example (see the full text at the end). I think I have got it almost right,
but when I try to run it in Xalan against my XML files I get the following
exception:

file:///data/disk21/tab-output.xslt; Line #71; Column #79; XSLT Error
(javax.xml.tran sform.Transform erConfiguration Exception):
javax.xml.trans form.Transforme rException: org.xml.sax.SAX ParseException:
Open quote is expected for attribute "select" associated with an element
type "xsl:value-of".
Exception in thread "main" java.lang.Runti meException:
javax.xml.trans form.Transforme rException: org.xml.sax.SAX ParseException:
Open quote is expected for attribute "select" associated with an element
type "xsl:value-of".
at org.apache.xala n.xslt.Process. doExit(Process. java:1153)
at org.apache.xala n.xslt.Process. main(Process.ja va:1126)

As far as I can see it does not like my qoutes, but they are exactly like
yours. I run Xalan on a Linux server. The XSLT file is saved in Unix file
format and ANSI encoding using TextPad, and then copied as binary to the
server.

Any ideas how to fix this?

Thanks again - John

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

<!-- Stylesheet inspired by one by Hermann Peifer, EEA, March 2008 -->

<!-- Variable definition for tab delimited output -->
<xsl:variable name="newline" select="'&#x0A; '"/>
<xsl:variable name="tab" select="'&#x09; '"/>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">

<!-- Write out field names -->
<xsl:text>acc n</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>abg n</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>newrt </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>rtnam e</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>jt</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>ajt </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>vol </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>ino </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>volis s</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>yr</xsl:text <xsl:value-of select="$newlin e"/>

<xsl:apply-templates select="inspec/article"/>

</xsl:template>

<!-- Loop through each field -->
<xsl:template match="article" >
<xsl:value-of select="inspec/article/contg/accn"/ <xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/abng"/ <xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/rtypg/newrt"/ <xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/rtypg/rtng/rtname"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/jt"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/ajt"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/vol"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/ino"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/voliss"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/contg/crt/yr"/ <xsl:value-of
select=$newline "/>
</xsl:template>

</xsl:stylesheet>
Sep 27 '08 #3
I wrote:
As far as I can see it does not like my qoutes, but they are exactly like
yours. I run Xalan on a Linux server. The XSLT file is saved in Unix file
format and ANSI encoding using TextPad, and then copied as binary to the
server.

Any ideas how to fix this?
My apologies - there was a quote missing right at the end by newline. After
fixing this the tranformation now runs. Sadly the output only contains the
field name and a lot of tabs - none of the content is printed out (and there
is content in some elements that should be there).

The root element of my XML files is "inspec" and each file contains an
number of articles, which is next level down in the tree. The data I want to
extract is in child elements under article. An example XPATH is
"inspec/article/bibliog/jrefg/jrog/vid/vol". Am I somehow pointing
incorrectly or not calling the select correctly or ?

Thanks - John

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

<!-- Stylesheet inspired by one by Hermann Peifer, EEA, March 2008 -->

<!-- Variable definition for tab delimited output -->
<xsl:variable name="newline" select="'&#x0A; '"/>
<xsl:variable name="tab" select="'&#x09; '"/>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">

<!-- Write out field names -->
<xsl:text>acc n</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>abg n</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>newrt </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>rtnam e</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>jt</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>ajt </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>vol </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>ino </xsl:text <xsl:value-of select="$tab"/>
<xsl:text>volis s</xsl:text <xsl:value-of select="$tab"/>
<xsl:text>yr</xsl:text <xsl:value-of select="$newlin e"/>

<xsl:apply-templates select="inspec/article"/>

</xsl:template>

<!-- Loop through each field -->
<xsl:template match="article" >
<xsl:value-of select="inspec/article/contg/accn"/<xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/abng"/<xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/rtypg/newrt"/<xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/contg/rtypg/rtng/rtname"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/jt"/<xsl:value-of
select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/ajt"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/vol"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/ino"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/bibliog/jrefg/jrog/vid/voliss"/>
<xsl:value-of select="$tab"/>
<xsl:value-of select="inspec/article/contg/crt/yr"/<xsl:value-of
select="$newlin e"/>
</xsl:template>

</xsl:stylesheet>

Sep 27 '08 #4

"John Larson" wrote:
My apologies - there was a quote missing right at the end by newline.
After fixing this the tranformation now runs. Sadly the output only
contains the field name and a lot of tabs - none of the content is printed
out (and there is content in some elements that should be there).

The root element of my XML files is "inspec" and each file contains an
number of articles, which is next level down in the tree. The data I want
to extract is in child elements under article. An example XPATH is
"inspec/article/bibliog/jrefg/jrog/vid/vol". Am I somehow pointing
incorrectly or not calling the select correctly or ?

Thanks - John
After a lot of trial and error I figured out that I needed to shorten my
XPATHs in the value of select statements (e.g. from <xsl:value-of
select="inspec/article/contg/accn"/to <xsl:value-of
select="contg/accn"/>). It now works beautifully - thanks to Hermann for
pointing to this solution.

Best

- John
Sep 27 '08 #5

"John Larson" wrote:
My apologies - there was a quote missing right at the end by newline.
After fixing this the tranformation now runs. Sadly the output only
contains the field name and a lot of tabs - none of the content is printed
out (and there is content in some elements that should be there).

The root element of my XML files is "inspec" and each file contains an
number of articles, which is next level down in the tree. The data I want
to extract is in child elements under article. An example XPATH is
"inspec/article/bibliog/jrefg/jrog/vid/vol". Am I somehow pointing
incorrectly or not calling the select correctly or ?

Thanks - John
After a lot of trial and error I figured out that I needed to shorten my
XPATHs in the value of select statements (e.g. from <xsl:value-of
select="inspec/article/contg/accn"/to <xsl:value-of
select="contg/accn"/>). It now works beautifully - thanks to Hermann for
pointing to this solution.

Best

- John

Sep 27 '08 #6
John Larson wrote:
"John Larson" wrote:
>My apologies - there was a quote missing right at the end by newline.
After fixing this the tranformation now runs. Sadly the output only
contains the field name and a lot of tabs - none of the content is printed
out (and there is content in some elements that should be there).

The root element of my XML files is "inspec" and each file contains an
number of articles, which is next level down in the tree. The data I want
to extract is in child elements under article. An example XPATH is
"inspec/article/bibliog/jrefg/jrog/vid/vol". Am I somehow pointing
incorrectly or not calling the select correctly or ?

Thanks - John

After a lot of trial and error I figured out that I needed to shorten my
XPATHs in the value of select statements (e.g. from <xsl:value-of
select="inspec/article/contg/accn"/to <xsl:value-of
select="contg/accn"/>). It now works beautifully - thanks to Hermann for
pointing to this solution.

Best

- John
Good to hear that you got it right.

You could add a leading slash to your select attribute:
<xsl:apply-templates select="/inspec/article"/>

Then, the XSLT processor will only search for inspec/article elements, which are immediately under the root element, and not somewhere in the middle of the XML structure (which might not occur in your XML document, but being as specific as possible is always a good idea, as it also saves processing time).

I assume that each article has max. 1 accn, abgn, etc. elements. If not: you will only get the value of the first one.

Hermann
Sep 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2054
by: Don Garrett | last post by:
I have an XML document at the root of a directory tree that contains relative URIs to resources in a directory tree. During XSLT processing, these URI's can be used without any problems to access the various documents in the tree. However, when generating HTML output, the output <a href=""> tags need to be adjusted for the location of the document that is displaying them. I need to use relative URLs, since the documents need to be...
3
3216
by: Ray Tayek | last post by:
hi, trying to use an xslt to make an xslt. trying something like: <?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml H:\java\projects\spy1\spy\inputDocumentMap.xml?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/inputDocumentMap"> <xsl:element name="xsl:template" match="/inputDocument">...
3
1941
by: johkar | last post by:
What is the proper doctype syntax for XHTML transitional??? Strict? How come XSLT doesn't preserve XHTML when it is compiled (Xalan)? Meaning, <br /> tags become <br> <input /> tags become <input> etc. It seems I am dazed and confused? Any hints or clear online resources appreciated. John
2
2116
by: Taare | last post by:
Hi, I got <xsl:output method="html" encoding="iso-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system=" http://www.w3.org/TR/html4/strict.dtd"/> in my XSLT file. This should remove all XML related code and replace with HTML valid code, but with my commandline XSLT transformer(http://xmlsoft.org/XSLT/) it outputs a xmlns on elements I'v used <xsl:copy-of select="/some/node"> to produce. Is this how it should work, or is this...
2
6625
by: sam | last post by:
Hi, I've been buried in xsl and xslt articles for several days now, and am still unsure as to what I need to do... Basically, my vb.net app loads up an XML file from an external source (Input.xml). When the user is setting up the source, they can make changes to it to specify which columns to include for use in my app, they can rename columns, they can mark a column as being of a known datatype (my own custom datatypes, e.g. a Tree). ...
10
3600
by: Andy Fish | last post by:
hi, I have an XSLT which is producing XML output. many of the nodes in the output tree contain namespace declarations for namespaces that are used in the source document even though they are not used in the result document or the stylesheet also I find that (for namespaces that are referenced in the stylesheet) even if I put an explicit namespace declaration on the root element of the result
10
54979
by: linhardt | last post by:
I am wondering how to make ASP.NET output XML instead of HTML. Here is my situation: I am using IE as a front-end to browse company documents. Let's say, a user wants a Word document called foobar, then they'd do a http request for "getdoc.aspx?foobar" Now what getdoc.aspx does is it gets the Word document from the server file system, does an XSLT transform on it to hyperlink it to other
3
9614
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the difference between two source documents and output node-sets which are "different" (changed or new) to new XML files using xsl:result-document To describe the problem I have provided some example data below along with my a portion of my current...
5
571
by: rhino | last post by:
I am beginning to learn XML and XSLT from the w3schools tutorial and I'm having a lot more trouble doing something simple than I should be, namely writing a single blank space into a document. I am trying to separate two strings with a space and nothing I'm doing works. Here is my XSLT, which I think you'll agree is really simple:: <?xml version="1.0" encoding="Windows-1252"?> <xsl:stylesheet version="1.0"
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.