Skip Headers
Oracle® XML DB Developer's Guide
10g Release 2 (10.2)

Part Number B14259-02
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

26 User-Defined Repository Metadata

This chapter describes how to create and use XML metadata, which you associate with XML data and store in Oracle XML DB Repository.

This chapter contains these topics:

Overview of Metadata and XML

Data that you use is often associated with additional information that is not part of the content. You can use such metadata to group or classify data, in order to process it in different ways. For example, you might have a collection of digital photographs, and you might associate metadata with each picture, such as information about the photographic characteristics (color composition, focal length) or context (location, kind of subject: landscape, people).

An Oracle XML DB repository resource is an XML document that contains both metadata and data; the data is the contents of element Contents; all other elements in the resource contain metadata. The data of a resource can be XML, but it need not be.

You can associate resources in the Oracle XML DB repository with metadata that you define. In addition to such user-defined metadata, each repository resource also has associated metadata that Oracle XML DB creates automatically and uses (transparently) to manage the resource. Such system-defined metadata includes properties such as the owner and creation date of each resource.

Except for system-defined metadata, you decide which resource information should be treated as data and which should be treated as metadata. In the case of a photo resource, supplemental information about the photo is normally not considered to be part of the photo data, which is a binary image. For text, however, you sometimes have a choice of whether to include particular information in the resource contents (data) or keep it separate and associate it with the contents as metadata — that choice is often influenced by the applications that use or produce the data.

Kinds of Metadata – Uses of the Term

In addition to resource metadata (system-defined and user-defined), the term "metadata" is sometimes used to refer to the following:

  • An XML schema is metadata that describes a class of XML documents.

  • An XML tag (element or attribute name) is metadata that is used to label and organize the element content or attribute value.

You can associate metadata with an XML document that is the content of a repository resource in any of these ways:

  • You can add additional XML elements containing the metadata information to the resource contents. For example, you could wrap digital image data in an XML document that also includes elements describing the photo. In this case, the data and its metadata are associated by being in the contents of the same resource. It is up to applications to separate the two and relate them correctly.

  • You can add metadata information for a particular resource to the repository as the contents of a separate resource. In this case, it is up to applications to treat this resource as metadata and associate it with the data.

  • You can add metadata information for a resource as repository resource metadata. In this case, Oracle XML DB recognizes the metadata as such. Applications can discover this metadata by querying the repository for it. They need not be informed separately of its existence and its association with the data.

User-Defined Resource Metadata

Of these different ways of considering metadata, this chapter is about only the last of those just listed: user-defined resource metadata. Such metadata is itself represented as XML: it is XML data that is associated with other XML data, describing it or providing supplementary, related information.

User-defined metadata for resources can be either XML schema-based or not:

  • Resource metadata that is schema-based is stored in separate (out-of-line) tables. These are related to the resource table by the resource OID, which is stored in the hidden object column RESID of the metadata tables.

  • Resource metadata that is not schema-based is stored in a CLOB column (RESEXTRA) in the resource table.

You can take advantage of schema-based metadata, in particular, to perform efficient queries and DML operations on resources. In this chapter, you will learn how to perform the following tasks involving schema-based resource metadata:

  • Create and register an XML schema that defines the metadata for a particular kind of resource.

  • Add metadata to a repository resource, and update (modify) such metadata.

  • Query resource metadata to find associated content.

  • Delete specific metadata associated with a resource and purge all metadata associated with a resource.

In addition, you will learn how to add non-schema-based metadata to a resource.

You can generally use user-defined resource metadata just as you would use resource data. In particular, versioning and access control management apply.

Typical uses of resource metadata include workflow applications, enforcing user rights management, tracking resource ownership, and controlling resource validity dates.

Scenario: Metadata for a Photo Collection

To illustrate the use of schema-based resource metadata, we will consider metadata associated with photographic image files that are stored in repository resources. You can create any number of different kinds of metadata to be associated with the same resource. In the case of image files, we will create metadata for information about both 1) the technical aspects of a photo and 2) the photo subject or the uses to which a photo might be put. We will use these two kinds of associated metadata to query photo resources.

XML Schemas to Define Resource Metadata

We first define the metadata that we want to associate with each photo resource. XML Schema is our tool for defining XML data: for each kind (technique, category) of metadata we define, we create and register an XML schema.

Example 26-1 Register an XML Schema for Technical Photo Information

The following XML schema defines metadata used to describe the technical aspects of a photo image file. We use procedure DBMS_XMLSCHEMA.registerSchema to register the XML schema. In order to identify this schema as defining repository resource metadata, we use the ENABLE_HIERARCHY_RESMETADATA value for the enableHierarchy parameter. Resource contents (data) are defined by using value ENABLE_HIERARCHY_CONTENTS (the default value), instead.

The properties we define here are the image height, width, color depth, title, and brief description.

BEGIN
  DBMS_XMLSCHEMA.registerSchema(
    'imagetechnique.xsd',
    '<xsd:schema targetNamespace="inamespace"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xdb="http://xmlns.oracle.com/xdb"
                 xmlns="inamespace">
       <xsd:element name="ImgTechMetadata"
                    xdb:defaultTable="IMGTECHMETADATATABLE">
         <xsd:complexType>
           <xsd:sequence>
             <xsd:element name="Height"      type="xsd:float"/>
             <xsd:element name="Width"       type="xsd:float"/>
             <xsd:element name="ColorDepth"  type="xsd:integer"/>
             <xsd:element name="Title"       type="xsd:string"/>
             <xsd:element name="Description" type="xsd:string"/>
           </xsd:sequence>
         </xsd:complexType>
        </xsd:element>
      </xsd:schema>',
    enableHierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);
END;
/

Example 26-2 Register an XML Schema for Photo Categorization

The following XML schema defines metadata used to categorize a photo image file: to describe its content or possible uses. In this simple example, we define a single, general property for classification, named Category.

BEGIN
  DBMS_XMLSCHEMA.registerSchema(
    'imagecategories.xsd',
    '<xsd:schema targetNamespace="cnamespace"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xdb="http://xmlns.oracle.com/xdb"
                 xmlns="cnamespace">
       <xsd:element name="ImgCatMetadata"
                    xdb:defaultTable="IMGCATMETADATATABLE">
         <xsd:complexType>
           <xsd:sequence>
             <xsd:element name="Categories" type="CategoriesType"/>
           </xsd:sequence>
         </xsd:complexType>
        </xsd:element>
        <xsd:complexType name="CategoriesType">
          <xsd:sequence>
            <xsd:element name="Category" type="xsd:string" maxOccurs="unbounded"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:schema>',
    enableHierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);
END;
/

Notice that there is nothing in the XML schema definitions of metadata that restrict that information to being associated with any particular kind of data. You are free to associate any type of metadata with any type of resource. And multiple types of metadata can be associated with the same resource.

Notice, too, that the XML schema does not, by itself, define its associated data as being metadata — it is the schema registration that makes this characterization, through enableHierarchy value ENABLE_HIERARCHY_RESMETADATA. If the same schema were registered instead with enableHierarchy value ENABLE_HIERARCHY_CONTENTS (the default value), then it would define not metadata for resources, but resource contents with the same information. Of course, the same XML schema cannot be registered more than once under the same name.

Adding, Updating, and Deleting Resource Metadata

You can add, update, and delete user-defined resource metadata in the following ways:

Using the latter two methods, updating and deleting metadata are done in the same way as adding metadata. If you supply a complete Resource element for one of these operations, then keep in mind that each resource metadata property must be a child (not just a descendent) of element Resource — if you want multiple metadata elements of the same kind, you must collect them as children of a single parent metadata element. The order among such top-level user-defined resource metadata properties is unimportant and is not necessarily maintained by Oracle XML DB.

The separate PL/SQL procedures in package DBMS_XDB are similar in their use. Each can be used with either XML schema-based or non-schema-based metadata. Some forms (signatures) of some of the procedures apply only to schema-based metadata. Procedures appendResourceMetadata and deleteResourceMetadata are illustrated here with examples.

See Also:

Oracle Database PL/SQL Packages and Types Reference for information on the procedures in PL/SQL package DBMS_XDB

Using APPENDRESOURCEMETADATA to Add Metadata

You can use procedure DBMS_XDB.appendResourceMetadata to add user-defined metadata to resources.

Example 26-3 Add Metadata to a Resource – Technical Photo Information

This example creates a photo resource and adds XML schema-based metadata of type ImgTechMetadata to it, recording the technical information about the photo.

DECLARE
  returnbool BOOLEAN;
BEGIN
  returnbool := DBMS_XDB.createResource(
                  '/public/horse_with_pig.jpg',
                  bfilename('MYDIR', 'horse_with_pig.jpg'));
  DBMS_XDB.appendResourceMetadata(
    '/public/horse_with_pig.jpg',
    XMLType('<i:ImgTechMetadata
                 xmlns:i="inamespace"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="inamespace imagetechnique.xsd">
               <Height>1024</Height>
               <Width>768</Width>
               <ColorDepth>24</ColorDepth>
               <Title>Pig Riding Horse</Title>
               <Description>Picture of a pig riding a horse on the beach,
taken outside hotel window.</Description>
             </i:ImgTechMetadata>'));
END;
/

Example 26-4 Add Metadata to a Resource – Photo Content Categories

This example adds metadata of type ImgTechMetadata to the same resource as Example 26-3, placing the photo in several user-defined content categories.

BEGIN
  DBMS_XDB.appendResourceMetadata(
    '/public/horse_with_pig.jpg',
    XMLType('<c:ImgCatMetadata
                 xmlns:c="cnamespace"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="cnamespace imagecategories.xsd">
               <Categories>
                 <Category>Vacation</Category>
                 <Category>Animals</Category>
                 <Category>Humor</Category>
                 <Category>2005</Category>
               </Categories>
             </c:ImgCatMetadata>'));
END;
/

PL/SQL procedure successfully completed.
 
SELECT * FROM imgcatmetadatatable;
 
SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<c:ImgCatMetadata xmlns:c="cnamespace" xmlns:xsi="http://www.w3.org/2001/XMLSche
ma-instance" xsi:schemaLocation="cnamespace imagecategories.xsd">
  <Categories>
    <Category>Vacation</Category>
    <Category>Animals</Category>
    <Category>Humor</Category>
    <Category>2005</Category>
  </Categories>
</c:ImgCatMetadata>
 
1 row selected.

Using DELETERESOURCEMETADATA to Delete Metadata

You can use procedure DBMS_XDB.deleteResourceMetadata to delete specific metadata associated with a resource. To delete all of the metadata associated with a resource, you can use procedure DBMS_XDB.purgeResourceMetadata.

Example 26-5 Delete Specific Metadata from a Resource

This example deletes the category metadata that was added to the photo resource in Example 26-4. By default, both the resource link (REF) to the metadata and the metadata table identified by that link are deleted. An optional parameter can be used to specify that only the link is to be deleted; the metadata table is then left as is but becomes unrelated to the resource. In this example, the default behavior is used.

BEGIN
  DBMS_XDB.deleteResourceMetadata('/public/horse_with_pig.jpg',
                                  'cnamespace',
                                  'ImgCatMetadata');
END;
/

PL/SQL procedure successfully completed.
 
SELECT * FROM imgcatmetadatatable;
 
no rows selected

Using SQL DML to Add Metadata

An alternative to using procedure DBMS_XDB.appendResourceMetadata to add, update, or delete resource metadata is to update the RESOURCE_VIEW directly using DML statements INSERT and UPDATE. Adding resource metadata in this way is illustrated by Example 26-6.

Example 26-6 Add Metadata to a Resource Using DML with RESOURCE_VIEW

This example shows how to accomplish the same thing as Example 26-3 by inserting the metadata directly into RESOURCE_VIEW using SQL statement UPDATE. Other SQL DML statements may be used similarly.

UPDATE RESOURCE_VIEW
  SET RES = 
        insertChildXML(
          RES, 
          '/r:Resource', 
          'c:ImgCatMetadata', 
          XMLType('<c:ImgCatMetadata
                       xmlns:c="cnamespace"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="cnamespace imagecategories.xsd">
                     <Categories>
                       <Category>Vacation</Category>
                       <Category>Animals</Category>
                       <Category>Humor</Category>
                       <Category>2005</Category>
                     </Categories>
                   </c:ImgCatMetadata>'),
          'xmlns:r="http://xmlns.oracle.com/xdb/XDBResource.xsd"
           xmlns:c="cnamespace"')
  WHERE equals_path(RES, '/public/horse_with_pig.jpg') = 1;
/

SELECT * FROM imgcatmetadatatable;
 
SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<c:ImgCatMetadata xmlns:c="cnamespace" xmlns:xsi="http://www.w3.org/2001/XMLSche
ma-instance" xsi:schemaLocation="cnamespace imagecategories.xsd">
  <Categories>
    <Category>Vacation</Category>
    <Category>Animals</Category>
    <Category>Humor</Category>
    <Category>2005</Category>
  </Categories>
</c:ImgCatMetadata>
 
1 row selected.

The following query extracts the inserted metadata using RESOURCE_VIEW, rather than directly using metadata table imgcatmetadatatable (the actual result is not pretty-printed):

SELECT extract(RES,
               '/r:Resource/c:ImgCatMetadata',
               'xmlns:r="http://xmlns.oracle.com/xdb/XDBResource.xsd"                xmlns:c="cnamespace"')
  FROM RESOURCE_VIEW
  WHERE equals_path(RES, '/public/horse_with_pig.jpg') = 1;

EXTRACT(RES,'/R:RESOURCE/C:IMGCATMETADATA','XMLNS:R="HTTP://XMLNS.ORACLE.COM/XDB
--------------------------------------------------------------------------------
<c:ImgCatMetadata xmlns:c="cnamespace" 
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="cnamespace imagecategories.xsd">
  <Categories>
    <Category>Vacation</Category>
    <Category>Animals</Category>
    <Category>Humor</Category>
    <Category>2005</Category>
  </Categories>
</c:ImgCatMetadata>
 
1 row selected.

Using WebDAV PROPPATCH to Add Metadata

Another alternative to using procedure DBMS_XDB.appendResourceMetadata to add resource metadata is to use the PROPPATCH method of the WebDAV protocol. This is illustrated by Example 26-7. Metadata updates and deletions can be made similarly.

Example 26-7 Add Metadata with WebDAV PROPPATCH

This example shows how to accomplish the same thing as Example 26-4 by inserting the metadata using the WebDAV protocol PROPPATCH method. Using appropriate tools, your application creates such a PROPPATCH WebDAV request and sends it to the WebDAV server for processing.

To update user-defined metadata, you proceed in the same way. To delete user-defined metadata, the WebDAV request is similar, but it has D:remove in place of D:set.

PROPPATCH /public/horse_with_pig.jpg HTTP/1.1
Host: www.myhost.com
Content-Type: text/xml; charset="utf-8"
Content-Length: 609
Authorization: Basic dGRhZHhkYl9tZXRhOnRkYWR4ZGJfbWV0YQ==
Connection: close
 
<?xml version="1.0" encoding="utf-8" ?>
<D:propertyupdate xmlns:D="DAV:" xmlns:Z="http://www.w3.com/standards/z39.50/">
  <D:set>
    <D:prop>
      <c:ImgCatMetadata
          xmlns:c="cnamespace"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="cnamespace imagecategories.xsd">
        <Categories>
          <Category>Vacation</Category>
          <Category>Animals</Category>
          <Category>Humor</Category>
          <Category>2005</Category>
        </Categories>
      </c:ImgCatMetadata>
    </D:prop>
  </D:set>
</D:propertyupdate>

Querying Schema-Based Resource Metadata

When you register an XML schema using the enableHierarchy value ENABLE_HIERARCHY_RESMETADATA, an additional column, RESID, is added automatically to the XMLType tables used to store the metadata. This column stores the object identifier (OID) of the resource associated with the metadata. You can use column RESID when querying metadata, to join the metadata with the associated data.

You can query metadata in these ways:

The latter method is recommended, for performance reasons. Direct queries of the RESOURCE_VIEW alone cannot be optimized using XPath rewrite, since there is no way to determine whether or not target elements like Category are stored in the CLOB value or in an out-of-line table.

To improve performance further, create an index on each metadata column you intend to query.

Example 26-8 Query XML Schema-Based Resource Metadata

This example queries both kinds of photo resource metadata, retrieving the paths to the resources that are categorized as vacation photos and have title "Pig Riding Horse".

SELECT ANY_PATH
  FROM RESOURCE_VIEW rs, imgcatmetadatatable ct, imgtechmetadatatable tt
  WHERE existsNode(RES,
                   '/r:Resource/c:ImgCatMetadata/Categories/Category
                    [text()="Vacation"]',
                   'xmlns:r="http://xmlns.oracle.com/xdb/XDBResource.xsd"
                    xmlns:c="cnamespace"') = 1
    AND existsNode(RES, 
                   '/r:Resource/i:ImgTechMetadata/Title
                    [text()="Pig Riding Horse"]',
                   'xmlns:r="http://xmlns.oracle.com/xdb/XDBResource.xsd"
                    xmlns:i="inamespace"') = 1
    AND rs.RESID = ct.RESID
    AND rs.RESID = tt.RESID;

ANY_PATH
--------------------------
/public/horse_with_pig.jpg
 
1 row selected.

XML Image Metadata from Binary Image Metadata

In previous sections of this chapter we have used a simple user-defined XML schema that defines technical image metadata, imagetechnique.xsd, to illustrate ways of adding and changing repository resource metadata. That simple XML schema is not intended to be realistic with respect to technical photographic image information.

However, nearly all digital cameras now include image metadata as part of the binary image files they produce, and Oracle interMedia, which is part of Oracle Database, provides tools for extracting and converting this binary metadata to XML. Oracle interMedia XML schemas are automatically registered with Oracle XML DB Repository to convert binary image metadata of the followings kinds to XML data:

EXIF is the metadata standard for digital still cameras; EXIF metadata is stored in TIFF and JPEG image files. IPTC and XMP metadata is commonly embedded in image files by desktop image-processing software.

See Also:

  • Oracle interMedia User's Guide for information on working with digital image metadata, including examples of extracting binary image metadata and converting it to XML

  • Oracle interMedia Reference for information on the XML schemas supported by Oracle interMedia for use with image metadata

Adding Non-Schema-Based Resource Metadata

You store user-defined resource metadata that is not schema-based as a CLOB instance in the ResExtra element of the associated resource. The default XML schema for a resource has a top-level element any (declared with maxOccurs= "unbounded") that allows any valid XML data as part of the resource document; this metadata is stored in CLOB column RESEXTRA of the resource table.

The following skeleton shows the structure and position of non-schema-based resource metadata:

<Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd"
       <Owner>DESELBY</Owner>
       ... <!-- other system-defined metadata -->
       <!-- contents of the resource>
       <Contents>
         ...
       </Contents>
       <!-- User-defined metadata (appearing within different namespace) -->
       <ResExtra>
         <MyOwnMetadata xmlns="http://www.example.com/custommetadata">
           <MyElement1>value1</MyElement1>
           <MyElement2>value2</MyElement2>
         </MyOwnMetadata>
       <ResExtra>
     </Resource>

You can set and access non-schema-based resource metadata belonging to namespaces other than XDBResource.xsd by using any of the methods described previously for schema-based resource metadata. Example 26-9 illustrates this for the case of SQL DML operations, adding user-defined metadata directly to the <RESOURCE> document.

Example 26-9 Add Non-Schema-Based Metadata to a Resource

This example shows how to add non-schema-based metadata to a resource using SQL DML.

DECLARE
  res BOOLEAN;
BEGIN
  res := DBMS_XDB.createResource('/public/NurseryRhyme.txt',
                                 bfilename('MYDIR',
                                           'tdadxdb-xdb_repos_meta-011.txt'),
                                 nls_charset_id('AL32UTF8'));
UPDATE RESOURCE_VIEW
  SET RES = insertChildXML(RES, 
                           '/r:Resource/r:ResExtra', 
                           'n:NurseryMetadata', 
                           XMLType('<n:NurseryMetadata xmlns:n="nurserynamespace">
                                      <Author>Mother Goose</Author>
                                   <n:NurseryMetadata>'),
                           'xmlns:r="http://xmlns.oracle.com/xdb/XDBResource.xsd"
                            xmlns:n="nurserynamespace"')
  WHERE equals_path(RES, '/public/NurseryRhyme.txt') = 1;
END;
/

PL/SQL procedure successfully completed.
 
 
SELECT rs.RES.getClobVal() FROM RESOURCE_VIEW rs
  WHERE equals_path(RES, '/public/NurseryRhyme.txt') = 1;
 
RS.RES.GETCLOBVAL()
--------------------------------------------------------------------------------
<Resource xmlns="http://xmlns.oracle.com/xdb/XDBResource.xsd" Hidden="false" Inv
alid="false" Container="false" CustomRslv="false" VersionHistory="false" StickyR
ef="true">
  <CreationDate>2005-05-24T13:51:48.043234</CreationDate>
  <ModificationDate>2005-05-24T13:51:48.290144</ModificationDate>
  <DisplayName>NurseryRhyme.txt</DisplayName>
  <Language>en-US</Language>
  <CharacterSet>UTF-8</CharacterSet>
  <ContentType>text/plain</ContentType>
  <RefCount>1</RefCount>
  <ACL>
    <acl description="Public:All privileges to PUBLIC" xmlns="http://xmlns.oracl
e.com/xdb/acl.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sch
emaLocation="http://xmlns.oracle.com/xdb/acl.xsd                           http:
//xmlns.oracle.com/xdb/acl.xsd">
      <ace>
        <principal>PUBLIC</principal>
        <grant>true</grant>
        <privilege>
          <all/>
        </privilege>
      </ace>
    </acl>
  </ACL>
  <Owner>TDADXDB_META</Owner>
  <Creator>TDADXDB_META</Creator>
  <LastModifier>TDADXDB_META</LastModifier>
  <SchemaElement>http://xmlns.oracle.com/xdb/XDBSchema.xsd#text</SchemaElement>
  <Contents>
    <text>Mary had a little lamb
Its fleece was white as snow
and everywhere that Mary went
that lamb was sure to go
</text>
  </Contents>
</Resource>
 
1 row selected.

PL/SQL Procedures Affecting Resource Metadata

The following PL/SQL procedures perform resource metadata operations:

See Also:

Oracle Database PL/SQL Packages and Types Reference for detailed information on these PL/SQL procedures