xsl-list
[Top] [All Lists]

Re: [xsl] Finding the ID attribute of an XPath from XMLSchema

2007-07-21 19:41:58
Mike
Based on the syntax above, I wrote the code and gives an error,
context document undefined.

declare function id-from-element ( $element as element()? )  as xs:string? {
      data(($element/@*[id(.) is ..])[1])
} ;

let $dm := doc("/repository/content/3001")
let $ee:= id-from-element($dm//figure/picture[1])
return
  <net>
    {$ee}
  </net>

It gives me an error "context document undefined".

the XML is having the struct

<?xml version="1.0" encoding="UTF-8"?>

<sample1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="sample1.xsd">
        <data>Copyright MyCompany 2007</data>
        <figure>
                <picture no="IMAGE-1"/>
                <picture no="IMAGE-2"/>
                <picture no="IMAGE-3"/>
        </figure>
</sample1>

XSD is

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
elementFormDefault="qualified">
        <xs:element name="sample1">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element ref="data"/>
                                <xs:element ref="figure"/>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
        <xs:element name="data" type="xs:string"/>
        <xs:element name="figure">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element maxOccurs="unbounded" 
ref="picture"/>
                        </xs:sequence>
                </xs:complexType>
                </xs:element>
        <xs:element name="picture">
                <xs:complexType>
                        <xs:attribute name="no" use="required" type="xs:ID"/>
                </xs:complexType>
        </xs:element>
</xs:schema>


Thanks
Vijith

On 7/21/07, Michael Kay <mike(_at_)saxonica(_dot_)com> wrote:
Given a node $n, you can find the value of its ID attribute if it has one
using

$n/@*[id(.) is $n]

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: v vijith [mailto:vvijith(_at_)gmail(_dot_)com]
> Sent: 21 July 2007 19:16
> To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
> Subject: Re: [xsl] Finding the ID attribute of an XPath from XMLSchema
>
> Below is a snippet from the XMLSchema
>
> <xs:complexType name="nameType">
>               <xs:sequence>
>                       <xs:element minOccurs="0" ref="desig"/>
>               </xs:sequence>
>               <xs:attribute ref="dest" use="required"/>
>               <xs:attribute ref="id"/>
>               <xs:attribute ref="garageid"/>
>               <xs:attributeGroup ref="xlink:XLINKATT1"/>
>       </xs:complexType>
> <xs:attribute name="id" type="xs:ID"/>
>
> this means that in the XML, the nameType node can be
> identified by the id attribute as xs:attribute name=id is
> mapped to XS:ID.
>
> Likewise there can be many nodes in the XML that has ID
> associated to it.
>
> <xml>
> <root>
> <somenode>
> <nameType id="2" attrib2="34">text</nameType> <nameType
> id="3" attrib2="34">text</nameType> <nameType id="4"
> attrib2="34">text</nameType> </somenode> <anothernode
> attrib5="55"/> </root>
>
>
> In the XML, I perform some kind of XQuery and I get the
> output as root/somenode/anothernode/nameType.
> Once I get this node, I should be able to find if there is an
> ID attribute defined for this node in the XSD.
> In the above case, the id attribute in the nameType tag is
> the attribute as per the XML Schema.
>
> So I need the output as id for the xpath
> root/somenode/anothernode/nameType
>
> Hope I have explained it better!
>
> Well what I thought was this way as a solution 1. Parse the
> XMLSchema 2. Make a map of XPath as key and the id as attribute.
> 3. Once I get the xpath from xquery, check the map to find
> the id 4. But parsing XMLSchema and mapping is not that simple either
>
> Thanks
> Vijith
>
> On 7/21/07, Michael Kay <mike(_at_)saxonica(_dot_)com> wrote:
> > > I have an XML which is associated to an XMLSchema. Once I
> perform an
> > > XQuery on the XML I get a selected set of XPaths (Im
> using our own
> > > functions).
> > >
> > > For each XPath I need to know what is the ID attribute
> for the XPath.
> > > I know that in XMLSchema there is an XS:ID Attribute. I
> would like
> > > to know if there is a way to find the ID associated from the
> > > XMLSchema for an XPath in the XML.
> > >
> > > Did I confuse everyone?
> >
> > You certainly confused me. Show us your source document and tell us
> > what you want your output to look like.
> >
> > Michael Kay
> > http://www.saxonica.com/
> >
> >
> >
> --~------------------------------------------------------------------
> > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> > --~--
> >
> >
>
> --~------------------------------------------------------------------
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
> --~--
>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--