1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
-
- This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
- project.
-
- Copyright (C) 1998-2018 OpenLink Software
-
- This project is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; only version 2 of the License, dated June 1991.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-
-->
<!--<chapter label="webandxmlview.xml" id="webandxmlview">
<title>XML Support</title> -->
<sect1 id="xmlservices">
<title>Virtuoso XML Services</title>
<sect2 id="xpath_sql">
<title>XPATH Implementation and SQL</title>
<para>
Virtuoso offers XPATH as a query language for XML views.
The statement is there converted into SQL in the context of the mapping defined by the __view XPATH option, which is mandatory.
An XPATH query string is a valid top level SQL statement. This is interpreted as a single select or union of selects with the result columns being specified by various XPATH options.
</para>
<para>
The basic query string
</para>
<programlisting>
XPATH [__view "cat"]/category
</programlisting>
<para>
will select any top level category elements from the cat XML view, defined with CREATE XML VIEW. This has a single result column with the serialization string of the selected entity as value. This string starts with the category start tag and ends with the
corresponding end tag. As many result rows are generated as there are top level category nodes in the view.
</para>
<para>
This basic behaviour can be modified by XPATH options enclosed in brackets after the XPATH keyword. These options allow specifying the output columns of the generated select statement.
</para>
</sect2>
<sect2 id="XPATHopts">
<title>XPATH Query Options</title>
<!-- Note undocumented __sax | __shallow | __doc STRING | -->
<programlisting>
<xp option> ::=
__* | __http | __key | __view NAME | __tag NAME | __quiet | __base_uri STRING |
xmlns:NAME '=' STRING | xmlns '=' STRING |
__lang STRING | __enc STRING | KEYWORD '=' KEYWORD
<option list> ::= | <option list> '[' <option> [...] ']'
</programlisting>
<para>
The option list may occur between the XPATH keyword and the start of the path,
e.g.
</para>
<programlisting>
XPATH [__key __view "cat"] /category
</programlisting>
<para>The effects of the options are as follows:
</para>
<formalpara>
<title>__http</title>
<para>Send the serialization of the result entities to the HTTP client. This may only be used inside a VSP page context.
</para>
</formalpara>
<formalpara>
<title>__key</title>
<para>Select the key of the selected entities instead of the serialization text.
</para>
</formalpara>
<formalpara>
<title>__*</title>
<para>Select all columns of the selected entity instead of its serialization text. This is only valid when __view is specified and the result set is homogeneous.
</para>
</formalpara>
<formalpara>
<title>__view STRING</title>
<para>Specify that the query be interpreted in the context of the specified CREATE XML VIEW, directly accessing the tables.
This is mandatory since the elements referenced can only be mapped to tables in the context of an XML view.
</para>
</formalpara>
<formalpara>
<title>__quiet</title>
<para>Specify that the query should not signal non-fatal errors. This option is for cases when an incomplete result is anyway better than nothing.
Typical example is search in the collection of documents where not all documents are valid.
</para>
</formalpara>
<formalpara>
<title>__base_uri STRING</title>
<para>Specify the base URI that can be used to resolve relative URIs in calls of XPATH functions
<link linkend="xpf_processXQuery"><function>processXQuery()</function></link>,
<link linkend="xpf_processXSLT"><function>processXSLT()</function></link> and
<link linkend="xpf_processXSQL"><function>processXSLT()</function></link>.
This is similar to the effect of "declare base-uri" in XQuery.
</para>
</formalpara>
<!--
<formalpara>
<title>__sax</title>
<para></para>
</formalpara>
<formalpara>
<title>__shallow</title>
<para></para>
</formalpara>
<formalpara>
<title>__doc STRING</title>
<para></para>
</formalpara>
-->
<formalpara>
<title>xmlns:NAME '=' STRING</title>
<para>This declares a pair of namespace prefix and namespace URI for use in the query expression,
e.g. xmlns:xs="http://www.w3.org/2001/XMLSchema" or xmlns:ora="http://schemas.oracle.com/xpath/extension"</para>
</formalpara>
<formalpara>
<title>xmlns '=' STRING</title>
<para>This declares the default namespace URI for use in the query expression,
e.g. xmlns="http://www.example.com/my-schema". This namespace will become both default element namespace and default function namespace.</para>
</formalpara>
<formalpara>
<title>__lang STRING</title>
<para>This declares the language that is used for text search expressions.
This is for internationalization purposes only. See subsection <link linkend="langfuncapi">"Adding New Languages And Encodings Into Virtuoso"</link>
for more details.
</para>
</formalpara>
<formalpara>
<title>__enc STRING</title>
<para>This declares encoding of strings that are used for text search expressions.
This is for internationalization purposes only. See subsections <link linkend="encodingxpathexp">"Encoding in XPath Expressions"</link>
and <link linkend="langfuncapi">"Adding New Languages And Encodings Into Virtuoso"</link> for more details.
</para>
</formalpara>
<formalpara>
<title>KEYWORD '=' KEYWORD</title>
<para>This is useful only if the expression uses XPATH functions like <link linkend="xpf_document"><function>document()</function></link>
or <link linkend="xpf_document"><function>doc()</function></link> or if the XPATH expression is an argument of
<link linkend="xpathcontainspredicate"><function>xpath_contains</function></link> or similar special SQL predicate.
These are configuration options that XPATH processor provides to the XML parser when the processor should read a document.
Section <link linkend="dtd_config">"Configuration Options of the DTD Validator"</link> lists all supported options.
</para>
</formalpara>
</sect2>
<sect2 id="xmlviews1">
<title>XML Views - Representing SQL Data as Dynamic and Persistent XML</title>
<para>
The XML view mechanism allows generating XML content from relational data and to query relational
data as if it were XML without first converting it to XML.
</para>
<sect3 id="createxmlview">
<title>CREATE XML VIEW statement </title>
<programlisting>
<xml view> ::=
CREATE XML VIEW <name> as [ <namespaces_def> ] <element list> <opt_public>
<xml view> ::=
CREATE XML VIEW <name> as [ <namespaces_def> ] <query spec> [ELEMENT] <opt_public>
<namespaces_def> ::= '[' <id_namespace> '='( name | <path string> ) [ ...] ']'
<element list> ::= <element> [, ...]*
<element> ::=
<table> <correlation name> AS [ <id_namespace> ':' ] <element> <columns>
[ ON '(' <search condition> ')']
[primary key '(' column_commalist ')' ]
[ELEMENT]
[ '{' <element list>'}' ]
<opt_public> ::= PUBLIC <path string> OWNER <DAV owner name> [PERSISTENT] [INTERVAL <minutes>]
<columns> ::= '(' <column> [, ...] ')'
<column> ::= <column name> | <column name> AS [ <id_namespace> ':' ] <attribute name>
<id_namespace> :: = identifier
</programlisting>
<para>
The XML view declaration establishes a 'virtual document' a context within
which XML hierarchy relationships can be translated into arbitrary joins. The virtual
document can be then materialized into an actual set of persistent XML elements or used to generate
SQL from XPATH.
</para>
<para>
Each table in the declaration generates an element into the result
document. SQL views can be used as tables to accommodate for hidden joins, sub-queries, ordering and
aggregates. If a view is used, which by nature has no primary key, the primary key
clause should be used to define a uniquely identifying set of view columns.
</para>
<para>
The only restriction on the XML view declaration is that each branch has a fixed
depth.
</para>
<para>
The structure of joins used to make the text can be specified in two ways: As a SQL query specification,
that is a SELECT from a list of tables with a WHERE clause specifying the joins, or as a tree of
join elements. The first form is called automatic and the second is called explicit. The automatic form allows
generating a tree with as many levels as there are tables in the join, with elements derived from the rows at each
level occupying each level of the hierarchy
</para>
<para>
With both forms the columns of the tables are mapped into either attributes or child elements
of the element representing each row. In the explicit mode, the attribute / element choice can be made for
each table, in the automatic mode for the entire view. The explicit mode also allows specifying a different
element / attribute name whereas the automatic mode takes the name from the column name. Even if the
columns are presented as child elements in the output text, they should be referenced as attributes in
XPATH queries evaluated in the context of the view.
</para>
<para>
An XML namespaces can be used in XML view with two restrictions -
all namespace names must be different and
the first three letters of namespace name must not be 'xml', except for
default namespace with name 'xmlns'.
</para>
</sect3>
<sect3 id="explicitxmlviews">
<title>Explicit XML Views </title>
<para>
In the explicit form each level of the hierarchy is declared as a list of child elements. Each such element maps
one table or view into an entity according to a join condition. The join conditions
can reference columns from the associated table and columns from tables in parent elements.
The join condition can also have scalar filtering conditions. A top element's join
condition may only specify scalar conditions.
</para>
<para>
Each set of sibling child nodes is delimited by braces {}. The top
level of the view typically consists of one element in the outermost braces. This element
has itself a child list delimited by braces. Each such list can have more than
one different element.
</para>
<para>
Each element specifies:
</para>
<itemizedlist mark="bullet">
<listitem>
<para>SQL table</para>
</listitem>
<listitem>
<para>Correlation name for use in subsequent joins for this table</para>
</listitem>
<listitem>
<para>XML element name to use for delimiting a row of this table</para>
</listitem>
<listitem>
<para>List of columns, with optional XML element or attribute names</para>
</listitem>
<listitem>
<para>join condition - will relate rows of this table to rows of the table in the enclosing element.
If this element is at the top level, this can only consist of scalar conditions</para>
</listitem>
<listitem>
<para>Optional PRIMARY KEY clause, needed if the table in this element is a view, does not
have a primary key or if a non-primary key unique identity is desired</para>
</listitem>
<listitem>
<para>Optional ELEMENT flag</para>
</listitem>
<listitem>
<para>Optional list of child elements, delimited by braces</para>
</listitem>
</itemizedlist>
<note>
<title>Note:</title>
<para>A correlation name is mandatory for all the tables.</para>
</note>
<para>
The column list can mention a single column or a single column renamed into an
XML attribute of a different name. If a column of a table is referenced in a subsequent join
condition it must appear in the output columns list. Expressions are not directly allowed
but a view with expression columns can be used.
</para>
<para>
The opt_public clause, when present, offers a shorthand for calling xml_view_publish
at the same time as making the definition. This makes a DAV resource reflecting the contents
of the view. The contents may either be generated on demand or persisted as a DAV accessible XML document.
In the latter case the document may be regenerated at a fixed interval. The interval is
expressed in minutes.
</para>
<para>
The path is expressed as an absolute path from the root collection of the DAV server.
</para>
<note>
<title>Note:</title>
<para>This root collection may be mapped into various places in the web server's URL space.
</para>
</note>
<programlisting>
create xml view xx ... public '/xx.xml' owner 'dav' persistent interval 1;
</programlisting>
<para>
is equivalent to:
</para>
<programlisting>
create xml view xx ...;
xml_view_publish (xx, /xx.xml', 'dav', 1, 1);
</programlisting>
<para>
A DAV resource created in this manner can be deleted as any DAV resource. The XML view
itself is not affected but a possibly existing refresh job will be automatically deleted.
</para>
<para>
One XML view can be published several times with different names and owners. There may also exist
persistent and non-persistent publications of the same view.
</para>
<para>
The CREATE XML VIEW statement defines stored procedures for generating an XML text fragment corresponding
to each element declared in the view.
</para>
<para>
The names of the procedures are composed as follows:
</para>
<programlisting>
create procedure http_view_<view name>
(inout output_mode)
</programlisting>
<programlisting>
create procedure http_<view name>_<element name>_<correlation name>
(in pk1 any, ..., in output_mode integer)
</programlisting>
<para>
An http output procedure is created for each <element> in the create xml view declaration. It takes
the primary key columns of the table in question in key order plus a mode flag. It then outputs the
serialization of the specified element and any child elements. For an output mode of 0 the result
goes directly to an HTTP client. For an output_mode of 1 the procedure returns the serialization as a string.
Note that for this to work the tables in question must be real tables and the join conditions must
only reference the next higher table in the create xml view tree. Further, the primary
key columns of each table should be mentioned in the columns list for that table along
with any foreign keys referenced in subsequent join conditions.
</para>
<example id="vht01">
<title>Examples</title>
<programlisting>
create xml view "cat" as
{
"Demo"."demo"."Categories" "C" as "category"
("CategoryID", "Description" as "description")
{
"Demo"."demo"."Products" "P" as "product" ("ProductName")
on ("P"."CategoryID" = "C"."CategoryID")
}
}
</programlisting>
<para>
This declares a two level hierarchy with a category node for each category
and a product child node for each product in the category.
</para>
<programlisting><![CDATA[
create xml view "cats_e" as
select "category"."CategoryID", "CategoryName",
"ProductName", "ProductID"
from "Demo".."Categories" "category", "Demo".."Products" as "product"
where "product"."CategoryID" = "category"."CategoryID" element;
]]></programlisting>
<para>Here is a similar example, this time using the element option.</para>
</example>
<para>
The procedures are
</para>
<programlisting>
xmlg_cat
http_cat_category_C (in categoryid any, in _out integer);
http_cat_product_P (in productid any, in _out integer);
</programlisting>
<para>
In the following example the function returns the selected items as an XML fragment.
Consecutive elements are separated by new-lines for readability.
</para>
<example id="vht02">
<title>Example</title>
<programlisting>
SQL> call "http_cat_category_C" (1, 1);
1 sets? Done. -- 5 msec.
RESULT=
<category CategoryID="1" description="Soft drinks, coffees, teas, beers, and ales" >
<product ProductName="Chai" ></product>
<product ProductName="Chang" ></product>
<product ProductName="Guarana Fantastica" ></product>
<product ProductName="Sasquatch Ale" ></product>
<product ProductName="Steeleye Stout" ></product>
<product ProductName="Côte de Blaye" ></product>
<product ProductName="Chartreuse verte" ></product>
<product ProductName="Ipoh Coffee" ></product>
<product ProductName="Laughing Lumberjack Lager" ></product>
<product ProductName="Outback Lager" ></product>
<product ProductName="Rhönbrooou Klosterbier" ></product>
<product ProductName="Lakkalikööri" ></product>
</category>
</programlisting>
</example>
<para>
The below example shows how to use a SQL view for hiding a join.
The below view generates for each table a set of column children and a set of
index children, which in turn have column children.
</para>
<programlisting>
create view KEY_COLS as select KP_KEY_ID, KP_NTH, C.*
from SYS_KEY_PARTS, SYS_COLS C where COL_ID = KP_COL;
create xml view "schema" as
{
DB.DBA.SYS_KEYS k as "table" ("KEY_TABLE" as "name",
KEY_ID as "key_id", KEY_TABLE as "table")
on (k.KEY_IS_MAIN = 1 and k.KEY_MIGRATE_TO is null)
{ DB.DBA.KEY_COLS c as "column" (COLUMN as name)
on (k.KEY_ID = c.KP_KEY_ID)
primary key (COL_ID),
DB.DBA.SYS_KEYS i as "index" (KEY_NAME
as "name", KEY_ID as "key_id", KEY_N_SIGNIFICANT as "n_parts")
on (i.KEY_TABLE = k.KEY_TABLE and i.KEY_IS_MAIN = 0 and i.KEY_MIGRATE_TO is null)
{
DB.DBA.KEY_COLS ic as "column" (COLUMN as "name")
on (ic.KP_NTH < i.KEY_N_SIGNIFICANT and ic.KP_KEY_ID = i.KEY_ID)
primary key (COL_ID)
}
}
};
</programlisting>
<para>
The following query will return the subtree describing the Customers table
in the demo database:
</para>
<programlisting>
XPATH [__view 'schema']
/table[@name = 'Demo.demo.Customers'];
<table name="0" key_id="1011" table="Demo.demo.Customers" >
<column name="CustomerID" ></column>
<column name="CompanyName" ></column>
<column name="ContactName" ></column>
<column name="ContactTitle" ></column>
<column name="Address" ></column>
<column name="City" ></column>
<column name="Region" ></column>
<column name="PostalCode" ></column>
<column name="Country" ></column>
<column name="Phone" ></column>
<column name="Fax" ></column>
<index name="City" key_id="1012" n_parts="2" >
<column name="City" ></column>
<column name="CustomerID" ></column>
</index>
<index name="CompanyName2" key_id="1013" n_parts="2" >
<column name="CompanyName" ></column>
<column name="CustomerID" ></column>
</index>
<index name="PostalCode2" key_id="1014" n_parts="2" >
<column name="PostalCode" ></column>
<column name="CustomerID" ></column>
</index>
<index name="Region" key_id="1015" n_parts="2" >
<column name="Region" ></column>
<column name="CustomerID" ></column>
</index>
</table>
</programlisting>
</sect3>
<sect3 id="xmlviewfromselect">
<title>Automatic XML Views - Creating XML Views from SELECT Statements</title>
<para>
The automatic form of CREATE XML VIEW will take a select statement
and infer a hierarchy from it, based on the order of tables in the from
clause. The parent table should be to the left of its children. This is practical
if the tables form a hierarchy in application terms, like orders and order lines or
departments and employees. This notation allows arbitrary depth but all siblings
at the same level will be of the same type. Elements of child rows will be
child elements of the element of their parent row, where the join condition
identifies the child rows for one parent row.
</para>
<para>
The columns in the selection will appear as attributes or child elements of the rows selected.
The names of the attributes will be the names of the columns. The names of the siblings will be the
names of the tables in the from clause, without qualifiers or owners. Expressions should not
appear in the selection. If the use of expressions is required then you may create a SQL view
first to facilitate this.
</para>
<para>
The ELEMENT keyword may be present at the end of the select, before
the publishing keywords. This will cause all columns to be represented as child
elements of the element corresponding to the row. Note that even if the element
switch is present, the values will appear like attributes in an XPATH query inside the view.
</para>
<note>
<title>Note</title>
<para>SQL views or derived tables may not appear directly in the select. The reason
for this is that a procedure is generated for each level of the generated XML tree and
that this must take unique identifying column values for the element in question.
If one desires to use a view, the explicit form should be used, with the primary key
option specified where appropriate.
</para>
</note>
<sect4 id="addtxt4xmlview">
<title>Add to text for explicit XML views</title>
<para>
Each set of sibling child nodes is delimited by braces {}. The top
level of the view typically consists of one element in the outermost braces. This element
has itself a child list delimited by braces. Each such list can have more than
one different element.
</para>
<para>
Each element specifies:
</para>
<itemizedlist mark="bullet">
<listitem>
<para>SQL table</para>
</listitem>
<listitem>
<para>Correlation name for use in subsequent joins for this table</para>
</listitem>
<listitem>
<para>XML element name to use for delimiting a row of this table</para>
</listitem>
<listitem>
<para>List of columns, with optional XML element or attribute names</para>
</listitem>
<listitem>
<para>join condition - will relate rows of this table to rows of the table in the enclosing element.
If this element is at the top level, this can only consist of scalar conditions</para>
</listitem>
<listitem>
<para>Optional PRIMARY KEY clause, needed if the table in this element is a view, does not
have a primary key or if a non-primary key unique identity is desired</para>
</listitem>
<listitem>
<para>Optional ELEMENT flag</para>
</listitem>
<listitem>
<para>Optional list of child elements, delimited by braces</para>
</listitem>
</itemizedlist>
<example>
<title>Add to examples:</title>
<programlisting>
create xml view "cats_e" as
select "category"."CategoryID", "CategoryName",
"ProductName", "ProductID"
from "Demo".."Categories" "category", "Demo".."Products" as "product"
where "product"."CategoryID" = "category"."CategoryID" element;
Add to text: after 'free text and xml'
</programlisting>
</example>
</sect4>
</sect3>
<sect3 id="xmlviewpublish">
<title>xml_view_publish</title>
<programlisting>
DB.DBA.xml_view_publish (in view_name varchar, in dav_path varchar,
in dav_owner varchar, in is_persistent integer, in refresh_interval integer)
</programlisting>
<para>
This presents an XML view as a DAV resource.
The view name is the name in the create xml view statement, note that this is case sensitive
and is never converted since it is a string, not an identifier.
The path must be absolute and is interpreted as relative from the DAV root collection.
The DAV user is the owner of the resource.
If is_persistent is non-zero the resource will be materialized from the view's description.
The refresh interval is only applicable if the resource is materialized. If so,
this is an interval in minutes. A value of 0 means no automatic refresh.
</para>
<para>
The reverse operation of xml_view_publish is deleting the DAV resource. xml_view_publish
may be called several times to alter the owner or refresh interval.
</para>
</sect3>
</sect2>
<!-- duplicated in webandxml.xml possibly? -->
<sect2 id="xmlviewextentref">
<title>External Entity References in Stored XML</title>
<para>
When an XML document is stored as either text, long xml, xmltype or in the persistent XML format it can
contain references to external parsed entities with the <!entity ...> declaration and
the &xx; syntax. These are stored as references and not expanded at storage
time if the entity is external.
</para>
<para>
Such references are transparently followed by XPATH and XSLT. A run time
error occurs if the referenced resource cannot be accessed when needed. The reference is only
followed if the actual subtree is selected by XPATH or XSLT. The resource is retrieved at most once
for each XPATH or XSLT operation referencing it, regardless of the number of times the link is traversed.
This is transparent, so that the document node of the referenced entity appears as if it were in the place of the
reference.
</para>
<para>
External entity references have an associated URI, which is either absolute with protocol identifier and full
path or relative. Relative references must be resolved with respect to the base URI
of the referencing document. If the document is stored as a column value in a table
it does not have a natural base URI, hence the application must supply one if relative references are to
be supported. This is done by specifying an extra column of the same table to contain a path, in the
form of collections delimited by slashes, just as the path of a DAV resource or a Unix file system path.
</para>
<para>
This base URI is associated with an XML column with the IDENTIFIED BY declaration:
</para>
<programlisting>
create table XML_TEXT (
XT_ID integer,
XT_FILE varchar,
XT_TEXT long varchar identified by xt_file,
primary key (XT_ID)
);
create index XT_FILE on XML_TEXT (XT_FILE);
</programlisting>
<para>
Thus, each time the value of xt_text is retrieved for XML processing by XPATH_CONTAINS or XCONTAINS
the base URI is taken from xt_file.
</para>
<para>
The complete URI for the xt_text of a column of the sample table will be:
</para>
<programlisting>
virt://<qualified table name>.<uri column>.<text column>:<uri column value>
</programlisting>
<para>
An example would be:
</para>
<programlisting>
"virt://DB.DBA.XML_TEXT.XT_FILE.XT_TEXT:sqlreference.xml"
</programlisting>
<para>
The .. and . in relative paths are treated as with file names when
combining relative references to base URI's. A relative reference without a path just
replaces the last part of the path in the base URI.
</para>
<tip>
<title>See</title>
<para>
<link linkend="fn_xml_uri_get">xml_uri_get and xml_uri_merge</link> for more details.
</para>
</tip>
</sect2>
<sect2 id="xpproc">
<title>Using XPATH in SQL Queries and Procedures</title>
<para>
An XPATH expression can appear as a SQL query expression,
that is, as a derived table or subquery predicate or scalar subquery.
This means that the XPATH expression is expanded compile time to the corresponding SQL.
The mapping of the XPATH hierarchy to tables and joins is given by the __view XPATH option, which is mandatory.
</para>
<para>
The XPATH keyword introduces an embedded XPATH expression. The XPATH
text is presented as a string literal. Note that the tokenization rules are different
for XPATH and SQL, so having XPATH as a string makes it clear which rules apply
to parsing which part of the composite query.
</para>
<example id="xp01">
<title>Example</title>
<programlisting>
select * from (XPATH '[__* __view "cat"]
//product') P order by "P."ProductName"";
</programlisting>
<para>
will evaluate the //product query in the context of the cat XML view
and produce a result set consisting of all the attributes of the
product entity as defined in the view.
</para>
</example>
<note>
<title>Note:</title>
<para>
The __key and __* XPATH options are central here in
defining the result columns of the XPATH. The default result column of an XPATH
expression is the serialization of the selected entity or scalar, which is most
of the time impractical in a SQL context.
</para>
</note>
<sect3 id="paramsinxp">
<title>Parameters in XPATH</title>
<para>
The '$' sign introduces a parameter in XPATH. The identifier following the dollar sign should reference a SQL column or variable defined in the surrounding context. The name of the parameter can contain a dot for referencing a column with a correlation name.
</para>
<para>
For instance, to make a VSP page that outputs the category tree which contains a specific product, one may write:
</para>
<programlisting>
<HTML>
<?vsp
declare N varchar;
N := {?'name'};
for (XPATH '[__http __view ''cat'']
/category[product/@ProductName = $N]' do ; ?>
</HTML>
</programlisting>
<para>
This will iterate over the categories containing a product with ProductName equal to
the URL parameter 'name'. Note the __http option that causes the text of the selected entities to go directly to the HTTP client.
Note the double '' escape for the XML view name inside the SQL string literal forming the name.
</para>
<para>
Also note that the N parameter is in upper case to work in all case modes. In some
modes SQL identifiers will be converted automatically to upper case but this conversion does not apply inside XPATH.
</para>
<programlisting>
select * from "Demo".."Categories" C
where exists (XPATH '[__view "ord"]
//products[@CategoryID = $C.CategoryID]');
</programlisting>
<para>
This example selects the categories of products that have been mentioned in the ord
XML view.
</para>
</sect3>
<note>
<title>Syntax Notes</title>
<para>
The main difference of SQL and XPATH is that the '-' is not a breaking
character in XPATH and that XPATH is case sensitive without any implicit
identifier case conversion.
</para>
</note>
</sect2>
<!-- !!!
<sect2 id="returningxml">
<title>XML Schema & DTD Functions</title>
&xml_auto_schema;
&xml_auto_dtd;
</sect2>
-->
<sect2 id="xquery_sql">
<title>XQUERY and XML view</title>
<para>
Similarly to XPATH, XQUERY may also be used as a query language for XML views.
Virtuoso offers a special case of FLWR expression for this purpose. It is possible
to use the <link linkend="xpf_xmlview"><function>xmlview()</function></link> function
in FOR clause expressions for querying XML views.
This function is similar to document () in the sense that it sets the
source of the path to be the logical root of the referenced XML view.
The XML view must be a constant known at compile time. A SQL query
against the appropriate tables of the XML view is internally
constructed and evaluated at run time, producing XML fragments from
the selected rows. At no point will non-selected parts of the
evaluation of the XML view be physically created. The path expression
following from <link linkend="xpf_xmlview"><function>xmlview()</function></link> may contain filters
involving XQuery variables bound in the scope of the path expression, thus allowing joining SQL data to
XQuery variable values.
</para>
<para>
The XQUERY string
</para>
<programlisting>
for $cat in xmlview("cat")/category return {$cat}
</programlisting>
<para>
in the query
</para>
<programlisting>
select xquery_eval(' {for $cat in xmlview("cat")/category return <q>{$cat}</q>}',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
is equivalent to the XPATH query string
</para>
<programlisting>
XPATH [__view "cat"]/category
</programlisting>
<para>described above.</para>
<para> The expression xmlview("viewname")/path is not a valid top level SQL statement, but may be
used by xquery_eval() function.
The path statement is translated into SQL query in the context of the "viewname" (i.e. the
necessary table names are taken from "viewname" XML view), so that only the desired relational data will be queried.
Functionality of this kind of SQL queries is
similar to functionality of the SQL fetch statement, i.e. such a query provides iteration over the result set
of a cursor. The query is executed once for each row in the query expression's result set.
Thus using the CREATE XML VIEW statement and a XQUERY FOR clause expression with
<link linkend="xpf_xmlview"><function>xmlview()</function></link> function allows you to query the database
and to return the results in the form of an XML document and to avoid redundant data access.
This kind of queries also allows computing joins between two or more documents and restructuring data.
</para>
<note>
<title>Note</title>
<para>
The <dummy_tag/> tag is not used and it is necessary only as an arbitrary argument for xtree_doc()
functions.
</para>
</note>
<example id="vht03">
<title>Example 1</title>
<programlisting>
create xml view "product" as
{
"Demo"."demo"."Products" p as "product"
("ProductID", "ProductName" as "product_name","UnitPrice" as "price",
"SupplierID","CategoryID")
{
"Demo"."demo"."Suppliers" s as "supplier" ("SupplierID","CompanyName")
on (s."SupplierID" = p."SupplierID")
,
"Demo"."demo"."Categories" c as "category" ("Description")
on (c."CategoryID" = p."CategoryID")
}
}
</programlisting>
</example>
<para>
This declares a two level hierarchy with a product node for each product
and a supplier child node and a category child node of the product.
</para>
<para>The following query will return the XML document in which each category node will contain all suppliers supplying products of the given category.
</para>
<programlisting>
select xquery_eval('
<document>
{
for $cat in xmlview("cat")/category
return (
<category description={$cat/@description}>
{
distinct (
for $prod in xmlview("product")/product
where $cat/@CategoryID=$prod/category/@CategoryID
return $prod/supplier )
}
</category> )
}
</document>
',
xtree_doc('<dummy_tag/>')
);
</programlisting>
<para>
returns the XML document in which each category node contains all suppliers supplying products of the given category.
</para>
<example id="vht04">
<title>Example 2</title>
<para>
Let a document named suppliers.xml contains supplier elements; each supplier element in turn contains
supplier_id and supplier_name subelements.
The following query
</para>
<programlisting>
select xquery_eval('
<supplier_product>
{
for $supp in document("suppliers.xml")/supplier
return (
<supplier>{$supp/supplier_name }
<product_name>
{
for $prod in xmlview("product")/product
where string($supp/supplier_id)=$prod/supplier/@SupplierID
return string($prod/@product_name)
}
</product_name>
</supplier>)
}
</supplier_product>',
xtree_doc('<dummy_tag/>')
);
</programlisting>
<para> returns the XML document that contains supplier elements; each supplier element in turn contains
supplier_name and product_name elements.
</para>
</example>
<para> The previous query and the following one show that
it is possible to use variables in a XPATH expression following <link linkend="xpf_xmlview"><function>xmlview()</function></link>
functions.
</para>
<example id="vht05">
<title>Example 3</title>
<para>The query </para>
<programlisting>
select xquery_eval('
<document>
{
distinct(
let $ex:= "Ex%"
for $prod in xmlview("product")//supplier[@CompanyName like $ex]
return <supp_id>{$prod/@SupplierID}</supp_id>)
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para> is equivalent to </para>
<programlisting>
select xquery_eval('
<document>
{
distinct(
for $prod in xmlview("product")//supplier [@CompanyName like "Ex%"]
return <supp_id>{$prod/@SupplierID}</supp_id>)
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para> and selects all suppliers having attribute "CompanyName" starting with "Ex".</para>
</example>
<sect3 id="optimizationinxq">
<title>Optimization in the queries with xmlview() function</title>
<para> At least two methods may be used to accelerate the execution of queries with xmlview() function.
The first method assumes that a path statement following xmlview() function should contain maximum conditions
to reduce the result set. For example, the query
</para>
<programlisting>
select xquery_eval('<w>
{
for $prod in xmlview("product")/product[@ProductID="1"]
return <q>{$prod}</q>
}</w>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>will be executed faster than </para>
<programlisting>
select xquery_eval('<w>
{
for $prod in xmlview("product")/product where $prod[@ProductID="1"]
return <q>{$prod}</q>
}</w>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>due to the SQL query produced from the path expression 'product[@ProductID="1"]' reduces
the result set in comparison with the SQL query produced from the path expression 'product' in the second
query.
</para>
<para>
If we execute a join of two (or more) XML views (or XML document and XML view), i.e. the query
consists of the nested loops, the second method proposes to carry out a piece of query of the nested loop
which is independent of the outer loop outside the outer loop and uses LET clause for it. For example, the
query
</para>
<programlisting>
select xquery_eval('<document>
{
let $prod_set:=(for $prod in xmlview("product")/product return $prod)
for $cat in xmlview("cat")/category
return (<category description={$cat/@description}$gt;
{distinct(for $prod in $prod_set
where $cat/@CategoryID=$prod/category/@CategoryID
return $prod/supplier)}</category>)}</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para> is equivalent to the query in the example 1, but it is about 5 times faster than original one.
This method is especially useful for full joins. In this case we do not have a full join and this
query may be optimized without the use of temporary result sets if
the 'where' clause is replaced with proper filter:
</para>
<programlisting>
select xquery_eval('<document>
{
for $cat in xmlview("cat")/category
let $catID := $cat/@CategoryID
return (<category description={$cat/@description}$gt;
{distinct(
for $supp in xmlview("product")/product[category/@CategoryID=$catID]/supplier
return $supp)
}</category>)
}
</document<',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
This variant requires no memory for storing $prod_set and it never fetches redundant
fields from "Demo"."demo"."Products" table but it heavily needs index for "Demo"."demo"."Products"
on "CategoryID" field. If such index is built the last variant is about 10 times faster than the
query in example 1. Similarly, the query in example 2 may be optimized as follows:
</para>
<programlisting>
select xquery_eval('
<supplier_product>
{
for $supp in document("suppliers.xml")/supplier
let $supp_id:=string($supp/@supplier_id)
return (
<supplier>{$supp/supplier_name}
<product_name>
{
for $prod in xmlview("product")/product[supplier/@SupplierID=$supp_id]
return string($prod/@product_name)
}
</product_name>
</supplier>)
}
</supplier_product>',
xtree_doc('<dummy_tag/>')
);
</programlisting>
<para> and it speeds up the operation by more than 15 times.
</para>
</sect3>
<sect3 id="restictionsinxq">
<title>Restrictions in XPATH expressions following the xmlview() function</title>
<para>Virtuoso does not support certain kinds of XPATH expressions applied to the xmlview() function.
</para>
<para>
1. A path expression must not contain any functions, because it is impossible to translate most of the functions
to SQL queries.
</para>
<para>
2. A path expression must not contain numeric XQUERY variables in the arithmetic expressions.
Let a document named products.xml contains product elements; each product element has numeric
attribute ProductPrice. A run time error occurs if the following query would be used
</para>
<programlisting>
select xquery_eval('
<document>
{
for $prod_doc in document("products.xml")/products/product/@ProductPrice
for $prod_view in xmlview("product")/product[@price>$prod_doc+1]
return <q>{$prod_doc, $prod_view/@product_name}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para> because the type of $prod_doc is considered as string. As it is mentioned in the previous restriction
the using a function in a path expression (e.g. xmlview("product")/product[@price>number($prod_doc)+1])
is not allowed.
The correct query is as follows:
</para>
<programlisting>
select xquery_eval('
<document>
{
for $prod_doc in document("products.xml")/products/product/@ProductPrice
let $prod_doc2:=number($prod_doc)
for $prod_view in xmlview("product")/product[@price>$prod_doc2+1]
return <q>{$prod_doc, $prod_view/@product_name}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
3. A path expression must not contain XQUERY variables with the following paths. The following query will not be
executed
</para>
<programlisting>
select xquery_eval('
<document>
{
for $cat in xmlview("cat")/category,
$prod in xmlview("product")/product[@CategoryID=$cat/@CategoryID]
return <q>{$prod/supplier}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
The correct query may be given as
</para>
<programlisting>
select xquery_eval('
<document>
{
for $cat in xmlview("cat")/category
let $cat_id:=$cat/@CategoryID
for $prod in xmlview("product")/product[@CategoryID=$cat_id]
return <q>{$prod/supplier}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
4. Virtuoso does not support a selection of n-th element in a path expression. The following query will not be executed
</para>
<programlisting>
select xquery_eval('
<document>
{
for $cat in xmlview("cat")//product[1] return <q>{$cat}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
5. Virtuoso does not support a dereference (=>) in a path expression.
</para>
<para>
6. It is not recommended to use the long varchar, long varbinary and long nvarchar data types with the logical and
boolean operations in a filter of the path expression.
For example, the execution of the following query
</para>
<programlisting>
select xquery_eval('
<document>
{
for $prod in
xmlview("product")/product[@SupplierID<5]/category[@Description like "Sw%"]
return <q>{$prod}</q>
}
</document>',
xtree_doc('<dummy_tag/>'));
</programlisting>
<para>
may return an error, because the field "Description" has LONG VARCHAR type in the table "demo"."Categories".
</para>
</sect3>
</sect2>
<sect2 id="mapping_schemas">
<title>Mapping Schemas as XML Views</title>
<para>
Virtuoso supports creating XML views by using annotated XSD
schemas referred to as mapping schemas.
A file containing a mapping schema may be loaded by calling the
<link linkend="fn_xml_load_mapping_schema_decl"><function>xml_load_mapping_schema_decl()</function></link>
function. A name (without extension .xsd) of the file containing a mapping schema
is considered to be the name of the xml view, defined by the given mapping schema.</para>
<para>A loaded mapping schema may be queried in the same way as one would
query XML views defined using the CREATE XML VIEW statement with XPATH:</para>
<programlisting>
XPATH [__view "xml_view_name"]/xpath_query
</programlisting>
<example id="ex_loadingmapxmlsch"><title>Loading and Querying a Mapping Schema</title>
<para> The XML view "Catmp" from the file "Catmp.xsd" may be loaded
using the following statement:</para>
<programlisting>
select xml_load_mapping_schema_decl (
'http://localhost.localdomain/xmlrepository/',
'Catmp.xsd',
'UTF-8',
'x-any' ) ) );
</programlisting>
<para>where the contents of "Catmp.xsd" is</para>
<programlisting>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:annotation>
<xsd:appinfo>
<sql:relationship name="CategoryProduct"
parent="Demo.demo.Categories"
parent-key="CategoryID"
child="Demo.demo.Products"
child-key="CategoryID" />
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="category" sql:relation="Demo.demo.Categories" type="CategoryType" />
<xsd:complexType name="CategoryType" >
<xsd:sequence>
<xsd:element name="product"
sql:relation="Demo.demo.Products"
sql:relationship="CategoryProduct" >
<xsd:complexType>
<xsd:attribute name="ProductName" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="CategoryID" type="xsd:integer" />
<xsd:attribute name="description" sql:field="Description" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
</programlisting>
<para>The XML view "Catmp" loaded from the file "Catmp.xsd" is similar to XML view "cat"
defined by CREATE XML VIEW in the section <link linkend="explicitxmlviews">Explicit Xml Views</link>.
</para>
<para>The query</para>
<programlisting>
XPATH [__view 'Catmp'] /category[@* = 1];
</programlisting>
<para>will now return the following result:</para>
<programlisting>
<category CategoryID="1" description="Soft drinks, coffees, teas, beers, and ales" >
<product ProductName="Chai" ></product>
<product ProductName="Chang" ></product>
<product ProductName="Guarana Fantastica" ></product>
<product ProductName="Sasquatch Ale" ></product>
<product ProductName="Steeleye Stout" ></product>
<product ProductName="Cote de Blaye" ></product>
<product ProductName="Chartreuse verte" ></product>
<product ProductName="Ipoh Coffee" ></product>
<product ProductName="Laughing Lumberjack Lager" ></product>
<product ProductName="Outback Lager" ></product>
<product ProductName="Rhonbrau Klosterbier" ></product>
<product ProductName="Lakkalikoori" ></product>
</category>
</programlisting>
</example>
<para>
Mapping schemas provide more flexibility than XML views defined by
the <computeroutput>CREATE XML VIEW</computeroutput> statement.
In the following mapping schema a constant element, "CustomerOrders", an
element that does not map to any database table or column but may appear in the
resulting XML as a container element of other subelements, is specified
by the <computeroutput>sql:is-constant</computeroutput> annotation.</para>
<programlisting>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:annotation>
<xsd:appinfo>
<sql:relationship name="CustOrders"
parent="Demo.demo.Customers"
parent-key="CustomerID"
child="Demo.demo.Orders"
child-key="CustomerID" />
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Customer" sql:relation="Demo.demo.Customers" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerOrders" sql:is-constant="1" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Order" sql:relation="Demo.demo.Orders"
sql:relationship="CustOrders"
maxOccurs="unbounded" >
<xsd:complexType>
<xsd:attribute name="OrderID" type="xsd:integer" />
<xsd:attribute name="OrderDate" type="xsd:date" />
<xsd:attribute name="CustomerID" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="CustomerID" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
</programlisting>
<para>After loading the "Cust_constant.xsd" file containing the given
mapping schema, the xpath query:</para>
<programlisting>
XPATH [__view 'Cust_constant'] /category[@* = 1];
</programlisting>
<para>will return the following result:</para>
<programlisting>
<Customer CustomerID="ALFKI" >
<CustomerOrders >
<Order CustomerID="ALFKI" OrderDate="1995-09-25 00:00:00.000000" OrderID="10643" ></Order>
<Order CustomerID="ALFKI" OrderDate="1995-11-03 00:00:00.000000" OrderID="10692" ></Order>
<Order CustomerID="ALFKI" OrderDate="1995-11-13 00:00:00.000000" OrderID="10702" ></Order>
<Order CustomerID="ALFKI" OrderDate="1996-02-15 00:00:00.000000" OrderID="10835" ></Order>
<Order CustomerID="ALFKI" OrderDate="1996-04-15 00:00:00.000000" OrderID="10952" ></Order>
<Order CustomerID="ALFKI" OrderDate="1996-05-09 00:00:00.000000" OrderID="11011" ></Order>
</CustomerOrders>
</Customer>
. . .
</programlisting>
<para>
Virtuoso does not support all mapping schema annotations at this time.
The following are currently unsupported:</para>
<simplelist>
<member><computeroutput>sql:encode</computeroutput></member>
<member><computeroutput>sql:use-cdata</computeroutput></member>
<member><computeroutput>sql:overflow-field</computeroutput></member>
<member><computeroutput>sql:inverse</computeroutput></member>
<member><computeroutput>sql:hide</computeroutput></member>
<member><computeroutput>sql:guid</computeroutput></member>
<member><computeroutput>sql:max-depth</computeroutput></member>
</simplelist>
<para>Also, there are some restrictions to the structure of mapping schemas: </para>
<simplelist>
<member>Attributes can not contain sql:relationship annotation.</member>
<member>Subelement having no sql:is-constant annotation and not mapped
to any table can not contain subelements and attributes.</member>
<member>Recursive relationships is not supported.</member>
</simplelist>
<note>
<title>Note:</title>
<para>
The XML views, defined by mapping schemas may not be queried
using XQUERY. They can however be referenced with the xmlview XPATH functions in path expressions inside an XQuery query.</para>
</note>
<tip><title>See Also:</title>
<para>SQLXML 3.0 documentation:
<ulink url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqlxml3/htm/ssxsdannotations_0gqb.asp">
Creating XML Views by Using Annotated XSD Schemas. </ulink>
</para></tip>
</sect2>
<sect2 id="view4xmldifferences">
<title>Differences Between SQLX, FOR XML and XML Views</title>
<para>
A SQLX or FOR XML query has no effect on the database schema. It is a
transient event and does not generate procedures or other schema elements.
</para>
<para>
These define an ad hoc mapping of a result set to XML.
There is no possibility of using XPATH to specify a search without first
constructing the whole tree. An XML view on the other hand
provides a mapping context in which one can make XQUERY or XPATH queries that are
mapped into SQL and the XML is only generated after applying the conditions.
</para>
<para>
XML views will usually be more efficient in complex cases and the notation there
may be simpler than the EXPLICIT notation in FOR XML.
For simple cases SQLX or FOR XML is the more convenient of the two.
SQLX or FOR XML does not restrict the SQL being used and will allow free use of
subqueries, expressions, derived tables, qualified joins etc.
</para>
</sect2>
</sect1>
<!--</chapter>-->
|