Re: [xml-dev] Checking for elements

From
Mike Fitzgerald <>
To
Noah Genner <>
Date
2004-05-25T16:46:47Z
ID
<>
Thread
Re: [xml-dev] Checking for elements
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,
>