You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Mukul Gandhi <mu...@apache.org> on 2011/05/03 18:02:10 UTC

Re: Parsing attributes from non-schema namespaces

I think this is why Xerces synthetic annotations should be used for.

Here's a code snippet (using your sample schema as input) illustrating
how to use synthetic annotations,

XMLSchemaLoader xsLoader = new XMLSchemaLoader();
xsLoader.setFeature("http://apache.org/xml/features/generate-synthetic-annotations",
true);
XSModel xsModel = xsLoader.loadURI("lineItemSchema.xsd");
XSComplexTypeDecl typeDefn =
(XSComplexTypeDecl)xsModel.getTypeDefinition("LineItemType",
"my-own-namespace");
XSParticle particle1 =
(XSParticle)(((XSModelGroup)(typeDefn.getParticle()).getTerm())).getParticles().item(0);
XSAnnotation annotation1 = ((XSElementDecl)particle1.getTerm()).getAnnotation();

i.e we need to set the feature
"http://apache.org/xml/features/generate-synthetic-annotations" to
'true' to enable generation of synthetic annotations.

In this case, an attribute in non XML Schema namespace (here,
yours:label="Name Label") is rendered as XSAnnotation object (variable
annotation1 in above code snippet). If we'll serialize the annotation
object annotation1, we would get something like follows,

<xs:annotation yours:label="Name Label"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my-own-namespace" xmlns:yours="your-namespace">
   <xs:documentation>SYNTHETIC_ANNOTATION</xs:documentation>
</xs:annotation>

Here's another workaround (just discovered this non standard &
undocumented method), for this requirement (in case you're willing to
make changes to schema document as suggested below),

i.e if you could specify the element declaration as follows (i.e write
a dummy/empty xs:annotation as child of xs:element),

<xs:element name="LineItemName" type="xs:string" yours:label="Name Label">
    <xs:annotation/>
</xs:element>

In this case, you don't have to specify the property
"http://apache.org/xml/features/generate-synthetic-annotations" (which
by default has value 'false').

Using the same code snippet as above, the annotation object
annotation1 would now look like as follows,

<xs:annotation yours:label="Name Label"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my-own-namespace" xmlns:yours="your-namespace">

</xs:annotation>

This is now a real schema annotation, and it also has your string
annotation information yours:label="Name Label".

I hope this helps.

On Tue, May 3, 2011 at 3:40 AM, Aziz Ibrahim <zi...@yahoo.com> wrote:
> Hello,
> I have what I think is a very simple question with regards to the Xerces 2
> Schema API. I need to parse an XML 1.0 schema that has embedded in it
> attributes from another namespace. See valid schema below. You can see the
> namespace for instance documents is "my". But, I have also defined a "yours"
> namespace and is used to further annotate the two elements "my:LineItemName"
> and "my:LineItemNumber" with labels for each. You see how I have added the
> yours:label attributes? When I parse this schema in Xerces 2, how can I get
> to these "extra" attributes? It looks to me like Xerces 2 simply ingores any
> attributes/elements that are not in the target or in the "xs" namespace. Is
> this true? If so, what other options do I have?
> Thanks in advance!
>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:my="my-own-namespace" targetNamespace="my-own-namespace"
> xmlns:yours="your-namespace" elementFormDefault="qualified">
>     <xs:element name="LineItemList" type="my:LineItemListType"/>
>     <xs:complexType name="LineItemListType">
>         <xs:sequence>
>             <xs:element name="LineItem" type="my:LineItemType"
> maxOccurs="unbounded"/>
>         </xs:sequence>
>     </xs:complexType>
>     <xs:complexType name="LineItemType">
>         <xs:sequence>
>             <xs:element name="LineItemName" type="xs:string"
> yours:label="Name Label"/>
>             <xs:element name="LineItemNumber" yours:label="Number Label"
> type="xs:string"/>
>             </xs:sequence>
>     </xs:complexType>
> </xs:schema>
>
> (A valid instance document for this is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <my:LineItemList xmlns:my="my-own-namespace">
>     <my:LineItem>
>         <my:LineItemName>XYZ Phone system</my:LineItemName>
>         <my:LineItemNumber>A12345</my:LineItemNumber>
>     </my:LineItem>
> </my:LineItemList>
> )
>





-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org