Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2008, 05:01 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again!

I still have no absolute solution for my problem. Here I have a small example of my files - xslt, xsd and xml. The xml file has to be transformed with the xslt parser and while transforming I would like to check if the new created file fits to the schema.
It actually works but only if I use <xsl:document validation="lax"> (see below) here. By trying to transform the same file with "strict" I get some error.

The problem is that the new xml file has to fit to the xsd schema completely. But by using lax I can also create some elements which are not in the schema without any errors.

Could anyone please tell me what am I doing wrong or where the mistake is?
xslt:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi ="http://www.w3.org/2000/10/XMLSchema-instance">
    <xsl:output method="xml" version="1.0" encoding="iso-8859-15" indent="yes"/>
    <xsl:import-schema namespace="http://en.intranet.fa.com/XMLSchema/project/V003" schema-location="..\XSD\schemafile\schema1.xsd"/>

<xsl:template match="/">    
    <xsl:document validation="strict">
        <xml_interface>
            <package_information>
                <xsl:copy-of select="/xml_interface/package_information/*"/>                
            </package_information>
                 </xml_interface>
    </xsl:document>
</xsl:template>

</xsl:stylesheet>
xml:
Code:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://en.intranet.fa.com/XMLSchema/project/V003" targetNamespace="http://en.intranet.fa.com/XMLSchema/project/V003" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
       <xs:element name="xml_interface">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="package_information">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="designation" type="xs:string" nillable="false"/>
                            <xs:element name="designer" type="xs:string" nillable="false"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
xml:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xml_interface xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <package_information>
        <designation>1</designation>
        <designer>H0587</designer>
    </package_information>
</xml_interface>
Thanks you a lot in advance!!

 
Old October 10th, 2008, 05:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your schema describes rules that apply to an element with local name xml_interface in namespace http://en.intranet.fa.com/XMLSchema/project/V003. But as far as I can see, the output you are generating contains an element with local name xml_interface in no namespace. So there is no element declaration for your output element in the schema, and therefore strict validation will fail.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 10th, 2008, 05:23 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your problem is namespaces. You are defining the schema in a namespace (http://en.intranet.fa.com/XMLSchema/project/V003) but neither the elements you are creating in the XSL, nor the elements in your input XML are in that namespace.

/- Sam Judson : Wrox Technical Editor -/
 
Old October 10th, 2008, 05:51 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you both! I was also thinking that the problem is somewhere there - in the namespace. But that is what I am not getting. Apologize, I am quite new on xslt. Can you tell me what exactly I have to change to make that work on this example??

 
Old October 10th, 2008, 06:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's not enough to change your XSLT code. Your source document is invalid against your schema, so you have to change either your source document or your schema. Assuming the schema is definitive and you want the data in a namespace, then change the source document to reference the namespace:

<xml_interface xmlns="http://en.intranet.fa.com/XMLSchema/project/V003">
  ...

and do the same in your stylesheet:

<xsl:template match="/">
    <xsl:document validation="strict">
        <xml_interface xmlns="http://en.intranet.fa.com/XMLSchema/project/V003">
            <package_information>
                <xsl:copy-of select="/xml_interface/package_information/*" xpath-default-namespace="http://en.intranet.fa.com/XMLSchema/project/V003"/>
            </package_information>
                 </xml_interface>
    </xsl:document>
</xsl:template>

You need to get the namespace right both on the elements you construct in the stylesheet, and on the path expressions that select nodes from the source document.

One minor comment, the specs say that names beginning "xml" are reserved for future standardization. Some products give you nasty warnings if you use such names, so they are best avoided.


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 10th, 2008, 06:16 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thank you! I will try that.
The problem is also that my xml files or source is actually fix. I can change the schema and the xslt parser.

 
Old October 10th, 2008, 06:21 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And I always get the same error:
Validation Error
The element declaration was not found for root element <xml_interface>.

 
Old October 10th, 2008, 06:30 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh I think that works!!!
It works on my small example anyway! Have to transfer that on my actual xslt file which is a little longer... but I guess I can do that now!

Thank you very much!!!

 
Old October 14th, 2008, 08:38 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello mhkay! Hello all!

I am still on the same problem: validation.
I did everything like you told me to do; I added xmlns="http://en.intranet.fa.com/XMLSchema/project/V003" everywhere like you describe that. But I assume that there might be a mistake or I am still doing something wrong. What I do is:

there is a source xml-file, a xslt-parser and a xsd-schema. I would like to transform the source xml-file into a new one which would then fit to the xsd-schema. The old one does not has to fit to the schema but only the new, which I create with the parser. But it seems like my xslt-parser checks if the source xml-file fits to the xsd-schema what is wrong. I still get errors about some elements which only exist in the old xml-file but neither in the new one or in the xsd-schema.


What I also tried to do was to transform the whole xml-file without validation and then just copy everything and validate that. I was thinking that should work. But now I get some strange errors like "value "" is not allowed in the element <xyz>". But it is according to the schema it is allowed. The whole element is even optional and at least there is no value ""(!) By the way it is the first element with the type boolean in the schema. Is there a special treatment for those?


Thank you in advance!

PS.: I use Altova XMLSpy 2008 pr.

 
Old October 14th, 2008, 08:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think you have reached the point where you are asking questions that are specific to the Altova product, and I can't help you very much with that (it's a competitor to my own product, and I have to try hard to resist saying nasty things about it...)

In particular there may be ways of controlling which documents are validated, but I couldn't find them when I looked.

If you want to try this with Saxon-SA then I will be able to help you, otherwise please contact Altova support.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transformation error rajashekhara XSLT 8 July 31st, 2008 05:00 AM
AddEditArticle.aspx - validation error emilypost BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 May 31st, 2007 02:55 PM
XML validation error alz6z XML 1 May 3rd, 2007 11:06 AM
ch05 no validation error humayunlalzad BOOK: Beginning XML 3rd Edition 2 November 15th, 2006 06:55 AM
xml validation error kreddy XML 0 August 20th, 2003 12:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.