[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Checking for elements


xsl test element not found
XSLT works for this kind of thing and its pretty easy, too. Say the file
data.xml is expected to contain the elements cow and chicken but 
it doesn't:

C:\XML\Examples>cat data.xml
<data>
 <pig/>
 <chicken/>
 <horse/>
</data>

Here's an XSLT stylesheet that checks for the the cow and chicken elements
in data.xml:

C:\XML\Examples>cat check.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="data">
<xsl:choose>
 <xsl:when test="not(chicken)">
  <xsl:message>chicken element not found</xsl:message>
 </xsl:when>
 <xsl:when test="not(cow)">
  <xsl:message>cow element not found</xsl:message>
 </xsl:when>
 <xsl:otherwise>
  <xsl:message>All needed elements found</xsl:message>
 </xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

If data.xml contains both cow and chicken, you'll get a report saying that
all the needed elements are found. If not, you get a complaint:

C:\XML\Examples>saxon data.xml check.xsl
cow element not found

Hope this works for you.

Mike

Noah Genner wrote:

> Hello All,
>  I'm new to all this and I'm looking for some direction.
>  I need to be able to check an XML document for some specific elements, and if
> possible produce a little report on which ones are missing. The document has
> already been validated against a dtd/schema that contains approximately 270
> elements, but I need to check that the ~40 I need are present. If they aren't
> present I want to produce a little report that says which elements are not present. 
>  Can I do this with XSLT, or is there an easier way to do this?   
> 
> Thanks,
> 

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.