document_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" name="Delta" price="800" stock="4" country="Denmark"/>
</products>
document_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:import-schema
schema-location="import-schema_1.xsd"/>
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<xsl:document
validation="strict">
<products>
<xsl:copy-of
select="products/product"/>
<product
id="p6" name="Romeo" price="2250" stock="5" country="South Africa"/>
</products>
</xsl:document>
</xsl:template>
</xsl:stylesheet>
document_1-schema.xsd
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element
name="products">
<xs:complexType>
<xs:sequence>
<xs:element
name="product" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute
ref="id" use="required"/>
<xs:attribute
name="name" type="xs:string" use="required"/>
<xs:attribute
name="price" type="xs:decimal" use="required"/>
<xs:attribute
name="stock" type="xs:integer" use="required"/>
<xs:attribute
name="country" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:attribute
name="id" type="xs:ID"/>
</xs:schema>
document_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products
validation="strict">
<product
id="p1" name="Delta" price="800" stock="4" country="Denmark"/>
<product
id="p6" name="Romeo" price="2250" stock="5" country="South Africa"/>
</products>
Note that the id attribute is now declared at global level in the xsd schema. For that reason it is not enough to use validation="strict" in the literal "products" element. We need xsl:document to make sure that the whole schema is taken into account.
FILENAMExslt-by-example.doc.xml
PAGE12/NUMPAGES55
Updated 2009-03-19