xsl:document

  1. document_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. document_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.   <xsl:document validation="strict">
  8.     <products>
  9.       <xsl:copy-of select="products/product"/>
  10.       <product id="p6" name="Romeo" price="2250" stock="5" country="South Africa"/>
  11.     </products>
  12.     </xsl:document>
  13.   </xsl:template>
  14. </xsl:stylesheet>
  1. document_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 ref="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:attribute name="id" type="xs:ID"/>
  20. </xs:schema>
  1. document_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <products validation="strict">
  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

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