dleslie 01/11/02 13:57:07
Added: java/samples/Validate birds.xml birds.xsl
ValidateXMLInput.java
Log:
Sample validates XML input.
Revision Changes Path
1.1 xml-xalan/java/samples/Validate/birds.xml
Index: birds.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Class [
<!ELEMENT Class (Order+)>
<!ELEMENT Order (Family+)>
<!ATTLIST Order Name CDATA #REQUIRED>
<!ELEMENT Family (Species+)>
<!ATTLIST Family Name CDATA #REQUIRED>
<!ELEMENT Species (#PCDATA)>
<!ATTLIST Species Scientific_Name CDATA #REQUIRED>
]>
<Class>
<Order Name="TINAMIFORMES">
<Family Name="TINAMIDAE">
<Species Scientific_Name="Tinamus major"> Great
Tinamou.</Species>
<Species Scientific_Name="Nothocercus">Highland Tinamou.</Species>
<Species Scientific_Name="Crypturellus soui">Little
Tinamou.</Species>
<Species Scientific_Name="Crypturellus cinnamomeus">Thicket
Tinamou.</Species>
<Species Scientific_Name="Crypturellus boucardi">Slaty-breasted
Tinamou.</Species>
<Species Scientific_Name="Crypturellus kerriae">Choco
Tinamou.</Species>
</Family>
</Order>
<Order Name="GAVIIFORMES">
<Family Name="GAVIIDAE">
<Speciesx Scientific_Name="Gavia stellata">Red-throated
Loon.</Speciesx>
<Species Scientific_Name="Gavia arctica">Arctic Loon.</Species>
<Species Scientific_Name="Gavia pacifica">Pacific Loon.</Species>
<Species Scientific_Name="Gavia immer">Common Loon.</Species>
<Species Scientific_Name="Gavia adamsii">Yellow-billed
Loon.</Species>
</Family>
</Order>
<Order Name="PODICIPEDIFORMES">
<Family Name="PODICIPEDIDAE">
<Species Scientific_Name="Tachybaptus dominicus">Least
Grebe.</Species>
<Species Scientific_Name="Podilymbus podiceps">Pied-billed
Grebe.</Species>
<Species Scientific_Name="">Atitlan Grebe.</Species>
<Species Scientific_Name="">Horned Grebe.</Species>
<Species Scientific_Name="">Red-necked Grebe.</Species>
<Species Scientific_Name="">Eared Grebe.</Species>
<Species Scientific_Name="">Western Grebe.</Species>
<Species Scientific_Name="">Clark's Grebe.</Species>
</Family>
</Order>
<Order Name="PROCELLARIIFORMES">
<Family Name="DIOMEDEIDAE">
<Species Scientific_Name="Thalassarche
chlororhynchos">Yellow-nosed Albatross. (A)</Species>
<Species Scientific_Name="Thalassarche cauta">Shy Albatross.
(A)</Species>
<Species Scientific_Name="Thalassarche melanophris">Black-browed
Albatross. (A)</Species>
<Species Scientific_Name="Phoebetria palpebrata">Light-mantled
Albatross. (A)</Species>
<Species Scientific_Name="Diomedea exulans">Wandering Albatross.
(A)</Species>
<Species Scientific_Name="Phoebastria immutabilis">Laysan
Albatross.</Species>
<Species Scientific_Name="Phoebastria nigripes">Black-footed
Albatross.</Species>
<Species Scientific_Name="Phoebastria albatrus">Short-tailed
Albatross. (N)</Species>
</Family>
<Family Name="PROCELLARIIDAE">
<Species Scientific_Name="Fulmarus glacialis">Northern
Fulmar.</Species>
<Species Scientific_Name="Pterodroma neglecta">Kermadec Petrel.
(A)</Species>
<Species Scientific_Name="Pterodroma arminjoniana">Herald Petrel.
(A)</Species>
<Species Scientific_Name="Pterodroma ultima">Murphy's Petrel.
(N)</Species>
<Species Scientific_Name="Pterodroma inexpectata">Mottled Petrel.
(A)</Species>
<Species Scientific_Name="Pterodroma cahow">Bermuda
Petrel.</Species>
<Species Scientific_Name="Pterodroma hasitata">Black-capped
Petrel.</Species>
<Species Scientific_Name="Pterodroma externa">Juan Fernandez
Petrel. (N)</Species>
<Species Scientific_Name="Pterodroma phaeopygia">Dark-rumped
Petrel.</Species>
<Species Scientific_Name="Pterodroma cervicalis">White-necked
Petrel. (H)</Species>
<Species Scientific_Name="Pterodroma hypoleuca">Bonin Petrel.
(H)</Species>
<Species Scientific_Name="Pterodroma nigripennis">Black-winged
Petrel. (H, A)</Species>
<Species Scientific_Name="Pterodroma cookii">Cook's Petrel.
(N)</Species>
<Species Scientific_Name="Pterodroma longirostris">Stejneger's
Petrel. (A)</Species>
<Species Scientific_Name="Bulweria bulwerii">Bulwer's Petrel.
(H)</Species>
<Species Scientific_Name="Bulweria fallax">Jouanin's Petrel. (H,
A)</Species>
<Species Scientific_Name="Procellaria parkinsoni">Parkinson's
Petrel. (N)</Species>
<Species Scientific_Name="Calonectris leucomelas">Streaked
Shearwater. (A)</Species>
<Species Scientific_Name="Calonectris diomedea">Cory's
Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus creatopus">Pink-footed
Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus carneipes">Flesh-footed
Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus gravis">Greater Shearwater.
(N)</Species>
<Species Scientific_Name="Puffinus pacificus">Wedge-tailed
Shearwater.</Species>
<Species Scientific_Name="Puffinus bulleri">Buller's Shearwater.
(N)</Species>
<Species Scientific_Name="Puffinus griseus">Sooty Shearwater.
(N)</Species>
<Species Scientific_Name="Puffinus tenuirostris">Short-tailed
Shearwater. (N)</Species>
<Species Scientific_Name="Puffinus nativitatis">Christmas
Shearwater. (H)</Species>
<Species Scientific_Name="Puffinus puffinus">Manx
Shearwater.</Species>
<Species Scientific_Name="Puffinus auricularis">Townsend's
Shearwater.</Species>
<Species Scientific_Name="Puffinus opisthomelas">Black-vented
Shearwater.</Species>
<Species Scientific_Name="Puffinus lherminieri">Audubon's
Shearwater.</Species>
<Species Scientific_Name="Puffinus assimilis">Little Shearwater.
(A)</Species>
</Family>
<Family Name="HYDROBATIDAE">
<Species Scientific_Name="Oceanites oceanicus">Wilson's
Storm-Petrel. (N)</Species>
<Species Scientific_Name="Pelagodroma marina">White-faced
Storm-Petrel. (A)</Species>
<Species Scientific_Name="Hydrobates pelagicus">European
Storm-Petrel. (A)</Species>
<Species Scientific_Name="Oceanodroma furcata">Fork-tailed
Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma leucorhoa">Leach's
Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma homochroa">Ashy
Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma castro">Band-rumped
Storm-Petrel. (N)</Species>
<Species Scientific_Name="Oceanodroma tethys">Wedge-rumped
Storm-Petrel. (N)</Species>
<Species Scientific_Name="Oceanodroma melania">Black
Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma macrodactyla">Guadalupe
Storm-Petrel.</Species>
<Species Scientific_Name="Oceanodroma markhami">Markham's
Storm-Petrel. (A)</Species>
<Species Scientific_Name="Oceanodroma tristrami">Tristram's
Storm-Petrel. (H)</Species>
<Species Scientific_Name="Oceanodroma microsoma">Least
Storm-Petrel.</Species>
</Family>
</Order>
<Order Name="PELECANIFORMES">
<Family Name="PHAETHONTIDAE">
<Species Scientific_Name="Phaethon lepturus">White-tailed
Tropicbird.</Species>
<Species Scientific_Name="Phaethon aethereus">Red-billed
Tropicbird.</Species>
<Species Scientific_Name="Phaethon rubricauda">Red-tailed
Tropicbird.</Species>
</Family>
<Family Name="SULIDAE">
<Species Scientific_Name="Sula dactylatra">Masked Booby.</Species>
<Species Scientific_Name="Sula nebouxii">Blue-footed
Booby.</Species>
<Species Scientific_Name="Sula variegata">Peruvian Booby.
(A)</Species>
<Species Scientific_Name="Sula leucogaster">Brown Booby.</Species>
<Species Scientific_Name="Sula sula">Red-footed Booby.</Species>
<Species Scientific_Name="Morus bassanus">Northern
Gannet.</Species>
</Family>
<Family Name="PELECANIDAE">
<Species Scientific_Name="Pelecanus erythrorhynchos">American
White Pelican.</Species>
<Species Scientific_Name="Pelecanus occidentalis">Brown
Pelican.</Species>
</Family>
<Family Name="PHALACROCORACIDAE">
<Species Scientific_Name="Phalacrocorax penicillatus">Brandt's
Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax brasilianus">Neotropic
Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax auritus">Double-crested
Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax carbo">Great
Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax urile">Red-faced
Cormorant.</Species>
<Species Scientific_Name="Phalacrocorax pelagicus">Pelagic
Cormorant.</Species>
</Family>
<Family Name="ANHINGIDAE">
<Species Scientific_Name="Anhinga anhinga">Anhinga.</Species>
</Family>
<Family Name="FREGATIDAE">
<Species Scientific_Name="Fregata magnificens">Magnificent
Frigatebird.</Species>
<Species Scientific_Name="Fregata minor">Great
Frigatebird.</Species>
<Species Scientific_Name="Fregata ariel">Lesser Frigatebird.
(A)</Species>
</Family>
</Order>
</Class>
1.1 xml-xalan/java/samples/Validate/birds.xsl
Index: birds.xsl
===================================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Class">
<BirdInfo>
<xsl:apply-templates select="Order"/>
</BirdInfo>
</xsl:template>
<xsl:template match="Order">
Order is: <xsl:value-of select="@Name"/>
<xsl:apply-templates select="Family"/><xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="Family">
Family is: <xsl:value-of select="@Name"/>
<xsl:apply-templates select="Species | SubFamily | text()"/>
</xsl:template>
<xsl:template match="SubFamily">
SubFamily is <xsl:value-of select="@Name"/>
<xsl:apply-templates select="Species | text()"/>
</xsl:template>
<xsl:template match="Species">
<xsl:choose>
<xsl:when test="name(..)='SubFamily'">
<xsl:text> </xsl:text><xsl:value-of select="."/><xsl:text>
</xsl:text><xsl:value-of select="@Scientific_Name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/><xsl:text> </xsl:text><xsl:value-of
select="@Scientific_Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
1.1 xml-xalan/java/samples/Validate/ValidateXMLInput.java
Index: ValidateXMLInput.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "XSLT4J" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, Lotus
* Development Corporation., http://www.lotus.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
/**
* Validate the XML input by using SAXParserFactory to turn on namespace
awareness and
* validation, and a SAX XMLReader to parse the input and report problems to
a very
* simple error handler.
*
* This sample uses birds.xml with an internal doctype declaration. As
shipped, birds.xml
* contains an element that violates the doctype.
*/
public class ValidateXMLInput
{
public static void main(String[] args)
throws Exception
{
ValidateXMLInput v = new ValidateXMLInput();
v.validateInput();
}
void validateInput()
throws Exception
{
// Since we're going to use a SAX feature, the transformer must support
// input in the form of a SAXSource.
TransformerFactory tfactory = TransformerFactory.newInstance();
if(tfactory.getFeature(SAXSource.FEATURE))
{
// Standard way of creating an XMLReader in JAXP 1.1.
SAXParserFactory pfactory= SAXParserFactory.newInstance();
pfactory.setNamespaceAware(true); // Very important!
// Turn on validation.
pfactory.setValidating(true);
// Get an XMLReader.
XMLReader reader = pfactory.newSAXParser().getXMLReader();
// Standard way of creating a transformer from a URL.
Transformer t = tfactory.newTransformer(
new StreamSource("birds.xsl"));
// Specify a SAXSource that takes both an XMLReader and a URL.
SAXSource source = new SAXSource(reader,
new InputSource("birds.xml"));
try
{
// Transform to a file.
t.transform(source, new StreamResult("birds.out"));
}
catch(TransformerException te)
{
// The operation failed, so parse the entire XML input to get a
complete listing of
// warnings and errors from the Handler inner class (see below).
Handler handler = new Handler();
reader.setErrorHandler(handler);
reader.parse("birds.xml");
// Note: If you are satisifed with reporting just the first
exception, you can
// eliminate the preceding 3 statements and Handler inner class, and
then report
// the first TransformerException as follows:
// System.out.println(te.getMessage());
}
System.out.println("Done!");
}
else
System.out.println("tfactory does not support SAX features!");
}
// Catch any errors or warnings from the XMLReader parse operation.
class Handler extends DefaultHandler
{
public void warning (SAXParseException spe)
throws SAXException
{
System.out.println("SAXParseException warning: " + spe.getMessage());
}
public void error (SAXParseException spe)
throws SAXException
{
System.out.println("SAXParseException error: " + spe.getMessage());
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]