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 September 23rd, 2008, 07:18 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transformation error? Validation

Hello comunity!

I am quite new in XSLT and hope to get some help here

What I am trying to do is to transform a xml-file to an other. Everything works so far. But I also have a xsd-schema and what I really need is that the new created xml-file is confirm to my schema. I hope I explain everything correctly - by using xslt I create a xml-file and this one has to have a namespace or something which says that it belongs to a special xsd-file. Like I would generate a sample xml file from schema so before you can save it the application proofs if everything in the new xml fits to the schema.

The other problem I have is that all elements in my source(old one) xml file without any value have the nillable attribute xsi:nil="true". After copying there is only nil="true" which seems to be a mistake. I guess there is something wrong with my xmlns:xs settings but i can not figure out what it is.


Thanks a lot in advance for any help!!

By the way, I am using AltovaXMLSpy

 
Old September 23rd, 2008, 07:33 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Altova XML Spy should support schema aware XSLT 2.0 so to validate your result against an XML schema you can use e.g.
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:import-schema
  namespace="http://example.com/2008/foo"
  schema-location="schema1.xsd"/>

<xsl:template match="/">
  <foo xmlns="http://example.com/2008/foo "xsl:validation="strict">
    <xsl:apply-templates/>
  </foo>
</xsl:template>

</xsl:stylesheet>
See http://www.w3.org/TR/xslt20/#validation for details.

--
Martin Honnen
Microsoft MVP - XML
 
Old September 23rd, 2008, 08:26 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Martin!

Thank you for your help so far.

Unfortunately it does not work yet. About your suggestion for exclude-result-prefixes="xs" - I am not sure if it is what I need, because I actually want the prefix xsi:nil="true" but what I get now is just nil="true", which is wrong.

About the import-schema - it seems like I still have a mistake there, too. I already checked the link you sent me, even before posting here. I could not solve my problem anyway... After http:// there supposed to be my location and in schema-location the path to my schema file? Or do both files have to be in the same folder? I tried that but I am still getting the error: Unable to locate a reference to a supported schema type (DTD, W3C Schema) within this document instance.

More suggestions?

Thanks in advance!

 
Old September 23rd, 2008, 09:04 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

First of all, my response so far did not in any way try to solve your xsi:nil problem, I was only addressing your validation problem.
If you need help with that xsi:nil problem then you need to provide details of your XML and XSLT.
As for validation, I don't use XML Spy but I have AltovaXML tools, the command line version of Altova's XSLT and XQuery engine.
With that engine I have no problems to validate the result of an XSLT transformation.
I will show you an example: Assume we have two XML input files, file1.xml looks as follows:
Code:
<root>
  <baz>1</baz>
  <baz>2</baz>
</root>
file2.xml as follows:
Code:
<root>
  <baz>1</baz>
  <baz>a</baz>
</root>
Then I have a schema schema1.xsd as follows:
Code:
<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://example.com/2008/foo"
  elementFormDefault="qualified">

  <xs:element name="foo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="bar" type="xs:int" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
and a stylesheet sheet1.xsl as follows:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://example.com/2008/foo"
  version="2.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:import-schema namespace="http://example.com/2008/foo" schema-location="schema1.xsd"/>

  <xsl:template match="/root">
    <foo xsl:validation="strict">
      <xsl:apply-templates select="baz"/>
    </foo>
  </xsl:template>

  <xsl:template match="baz">
    <bar>
      <xsl:apply-templates/>
    </bar>
  </xsl:template>

</xsl:stylesheet>
So what the stylesheet does is to transform one of the XML input documents to a result document and to validate that result against the schema. The schema requires the '{http://example.com/2008/foo}bar' element to be of type xs:int which will fail for the second input for the second element as that is not an integer but the string 'a'.
And in that case AltovaXML tools nicely reports
  "Validation Error
Value 'a' is not allowed for element <bar>. at foo"

If you have problems specific to Altova software then you might also want to try an appropriate forum on Altova's website http://www.altova.com/forum/forum_landingpage.aspx

--
  Martin Honnen
  Microsoft MVP - XML
 
Old September 23rd, 2008, 10:07 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you again for a very long answer! I will try that.

Do I understand you correctly that the validation proceed at the same time like the transformation? That might be what I misunderstood. So that after the transformation like it is in your example is finished the created file is valid to this schema anyway? Or do I misunderstand something again?

The thing is - what I did before: First I transformed the needed file like I need it to be. Than I created a sample xml file from my schema and copied that's header in the transformed xml file. After I did that and tried to save that file with it's new header the validation started.

So, what I was thinking is that there necessarily has to be that header in each transformed xml file. Am I wrong?


Thank you really a lot for your time you take to answer my questions!

 
Old September 23rd, 2008, 10:15 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The result elements the transformation creates are validated against the schema(s) the stylesheet imports. That happens during the transformation, either the transformation creates a valid result or it stops to report validation errors.

As for your attempts, I don't understand what kind of "header" you copied. You will have to show the details of your XML to enable us to understand what exactly you tried.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old September 23rd, 2008, 10:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First, do check that you are using the latest version of AltovaXML 2008. Some earlier versions were quite buggy but they seem to improve quite a bit from one release to the next.

You haven't shown your code, but one must assume that if you are getting the xsi:nil attribute in the wrong namespace that there is some error in your code. If you want to confirm this, try running the same code on Saxon and see if you get the same result.

When you use a schema-aware transformation then validation happens during the transformation. Potentially validation can even happen statically by analysis of your stylesheet, though I don't think Altova does this yet. Of course you could also do validation as a post-processing step separately from the transformation, but the advantage of doing it together is that potentially you get much better error messages, telling you what is wrong with your stylesheet rather than what is wrong with your output.

Generating an xsi:schemaLocation in a document is not the only way to get schema validation done. Most schema validators will allow you to specify separately where the instance document and the schema come from - after all, if you don't trust the instance to be valid, why should you trust it to tell you where the schema is? But XML Spy, I think, does place fairly heavy reliance on xsi:schemaLocation.

Michael Kay

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old September 23rd, 2008, 11:20 AM
Authorized User
 
Join Date: Sep 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your answers!

About the xsi:nil="true":

my source xml file looks like that:

        <first_name xsi:nil="true"/>
        <second_name xsi:nil="true"/>
        <some_no xsi:nil="true"/>

All elements without any value have such information. If I just copy that without changing anything I get the followings:

        <plant_tech_name nil="true"/>
        <plant_org_name nil="true"/>
        <plant_org_no nil="true"/>

I will also try it with Saxon, thanks for this tip. And I will post more about the other thing after I tried that.

Thank you so far a lot!

 
Old September 23rd, 2008, 01:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the attribute loses the namespace when you copy it then that would be a bug in the XSLT processor. Now you mention it, I seem to remember seeing something similar when testing some of the examples in the book against the Altova processor.

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

All right! Thank you both very much!
Now I have something to try and to work on. As soon as I know what my mistake was or if it was a bug I will post that.
I also contacted the Altova support. If that is a bug about wrong copying they will find that, I hope.

Greets
Alex






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.