xsl:import-schema

  1. import-schema_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" name="Delta" price="800" stock="4" country="Denmark"/>
  5. </products>
  1. import-schema_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <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">
  4.   <xsl:import-schema schema-location="import-schema_1.xsd"/>
  5.   <xsl:output indent="yes"/>
  6.   <xsl:template match="/">
  7.     <products xsl:validation="strict">
  8.       <xsl:copy-of select="products/product"/>
  9.       <product id="p6" name="Romeo" price="2250" stock="5" country="South Africa"/>
  10.     </products>
  11.   </xsl:template>
  12. </xsl:stylesheet>
  1. import-schema_1-schema.xsd

  2. <?xml version="1.0"?>
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  4.   <xs:element name="products">
  5.     <xs:complexType>
  6.       <xs:sequence>
  7.         <xs:element name="product" maxOccurs="unbounded">
  8.           <xs:complexType>
  9.             <xs:attribute name="id" type="xs:ID" use="required"/>
  10.             <xs:attribute name="name" type="xs:string" use="required"/>
  11.             <xs:attribute name="price" type="xs:decimal" use="required"/>
  12.             <xs:attribute name="stock" type="xs:integer" use="required"/>
  13.             <xs:attribute name="country" type="xs:string" use="required"/>
  14.           </xs:complexType>
  15.         </xs:element>
  16.       </xs:sequence>
  17.     </xs:complexType>
  18.   </xs:element>
  19. </xs:schema>
  1. import-schema_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <products>
  4.   <product id="p1" name="Delta" price="800" stock="4" country="Denmark"/>
  5.   <product id="p6" name="Romeo" price="2250" stock="5" country="South Africa"/>
  6. </products>

Comment

xsl:import-schema only works in a schema-aware XSLT processor. It makes input validation, validation of temporary trees and output validation possible. In our example the new product element is validated against the imported schema.

Updated 2009-03-19