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



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
.



Relevant Pages

  • Re: [Newbie] Output something using XSLT even if an element is missing in some documents?
    ... contains the field name and a lot of tabs - none of the content is printed ... 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. ... An example XPATH is ...
    (comp.text.xml)
  • Insering a node via an expression
    ... I am creating a simple CRUD application which uses xml files as data storage. ... I extended Textbox with a XPath expression and a XmlDocument property, this way I can easily read and update existing nodes' content. ... If my XmlDocument had a schema it would be somehow possible to construct a prototype xml fragment that I could insert in the right spot, but this in interaction with XPath goes way beyond my programming skills. ...
    (microsoft.public.dotnet.xml)
  • Re: Getting data insdie a tag
    ... > I'm just learning how to parse xml files. ... > the cdata between tags but am having trouble getting the extra info ... > from the tags. ... Use xpath. ...
    (alt.php)
  • Using C# to search several xml files using the least amount of memory as possible?
    ... I know I could take all the xml files and loaded them into a single ... XmlDocument and then use xpath to search for nodes. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: archival of newsfeeds?
    ... But the problem when I use xml files is that if I want to modifiy them, ... use the XPathDocument wich allows you to use XPath on ... If there is no need to modify anything, ... >> How fast is XPath? ...
    (microsoft.public.dotnet.xml)