XML Schema to validate against XML encryption

Discussion in 'XML' started by Stefan, Mar 28, 2008.

  1. Stefan

    Stefan Guest

    Hello,

    I'm trying to write a XML Schema to validate against a XML file which
    holds an element which contains either a plaint text password or a XML
    encryption element. A plain text example is:

    <Password>mySecret</Password>

    or a XML encrypted element:

    <Password>
    <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"
    Type="http://www.w3.org/2001/04/xmlenc#Content">
    <EncryptionMethod
    Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc"/>
    <CipherData>
    <CipherValue>Jha8fYGDje49Tkz0=</CipherValue>
    </CipherData>
    </EncryptedData>
    </Password>

    I tried to validate against it with following XML Schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
    elementFormDefault="qualified">

    <xsd:import namespace="http://www.w3.org/2001/04/xmlenc#"

    schemaLocation="http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd"/>

    <xsd:element name="Secret">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="EncryptedData" type="xenc:EncryptedDataType"
    minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>


    But it fails with an error message like this:

    element EncryptedData: Schemas validity error : Element
    '{http://www.w3.org/2001/04/xmlenc#}EncryptedData': This element is not
    expected. Expected is ( EncryptedData ).

    I don't know why the EncryptedData-Node isn't in the namespace of
    xmlenc.

    The problem is that the schema must support two types of <Password>. One
    time it could be a pure xsd:string and the other one could be a parent
    of EncryptedData.

    Any ideas how to solve that?

    Best regards
    Stefan
    Stefan, Mar 28, 2008
    #1
    1. Advertising

  2. Stefan wrote:

    > <Password>
    > <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"
    > Type="http://www.w3.org/2001/04/xmlenc#Content">


    In this sample the element with local name 'EncryptedData' is in the
    namespace http://www.w3.org/2001/04/xmlenc# while the schema below

    > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    > xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
    > elementFormDefault="qualified">
    >
    > <xsd:import namespace="http://www.w3.org/2001/04/xmlenc#"
    >
    > schemaLocation="http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd"/>
    >
    > <xsd:element name="Secret">
    > <xsd:complexType>
    > <xsd:sequence>
    > <xsd:element name="EncryptedData" type="xenc:EncryptedDataType"
    > minOccurs="0" maxOccurs="1"/>


    defines the element in no namespace. Therefore you get the validation error

    > element EncryptedData: Schemas validity error : Element
    > '{http://www.w3.org/2001/04/xmlenc#}EncryptedData': This element is not
    > expected. Expected is ( EncryptedData ).


    saying that. Thus if you want to define an element with local name
    'EncryptedData' in the namespace http://www.w3.org/2001/04/xmlenc# then
    you need to write a schema with that namespace as its targetNamespace,
    then you need to import that schema and use xs:element ref to reference
    the element from the imported schema.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 28, 2008
    #2
    1. Advertising

  3. On Fri, 2008-03-28 at 18:42 +0100, Martin Honnen wrote:
    [...]
    > saying that. Thus if you want to define an element with local name
    > 'EncryptedData' in the namespace http://www.w3.org/2001/04/xmlenc# then
    > you need to write a schema with that namespace as its targetNamespace,
    > then you need to import that schema and use xs:element ref to reference
    > the element from the imported schema.


    Isn't it possible to only change my schema. I wouldn't prefer to create
    a new external schema where I define a ref to that element. One (schema)
    file is mandatory for me.

    I just thought about something like that (specifying the namespace in
    the schema-element), but it doesn't work too:

    <xsd:element xmlns="http://www.w3.org/2001/04/xmlenc#"
    name="EncryptedData" type="xenc:EncryptedDataType" minOccurs="0"
    maxOccurs="1"/>

    I only want to check against a XML encrypted standard element.
    Stefan Schulze Frielinghaus, Mar 29, 2008
    #3
  4. Stefan Schulze Frielinghaus wrote:

    > Isn't it possible to only change my schema. I wouldn't prefer to create
    > a new external schema where I define a ref to that element. One (schema)
    > file is mandatory for me.


    You need one schema for each target namespace.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 29, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    573
  2. Paco
    Replies:
    4
    Views:
    3,562
  3. Leona
    Replies:
    9
    Views:
    1,988
    Henry S. Thompson
    Nov 1, 2004
  4. ric_deez
    Replies:
    0
    Views:
    1,061
    ric_deez
    Apr 3, 2006
  5. Shawn

    validate xml against schema

    Shawn, Jan 27, 2004, in forum: ASP General
    Replies:
    1
    Views:
    146
    Chris Hohmann
    Jan 27, 2004
Loading...

Share This Page