Presentation is loading. Please wait.

Presentation is loading. Please wait.

C10. SQL-S + XML Date Semistructurate, 2012-2013.

Similar presentations


Presentation on theme: "C10. SQL-S + XML Date Semistructurate, 2012-2013."— Presentation transcript:

1 C10. SQL-S + XML Date Semistructurate, 2012-2013

2 C10. DS SQL-S + XML

3 FOR Clause [ FOR { BROWSE | } ] ::= XML { { RAW [ ( 'ElementName' ) ] | AUTO } [ [, { XMLDATA | XMLSCHEMA [ ( ‘NS_URI‘ ) ] } ] [, ELEMENTS [ XSINIL ] ] | EXPLICIT [ [, XMLDATA ] ] | PATH [ ( 'ElementName' ) ] [ [, ELEMENTS [ XSINIL ] ] ] } ::= [, TYPE ] [, ROOT [ ( 'RootName' ) ] ] C10 - DS

4 BD Exemple: sectii (cods, denumires) discipline (codd, denumired, detalii) planinv (cods, codd) studenti (cods, nrmatricol, nume, grupa, datan, medie) rezultate (nrmatricol, disciplina, nota) Obs: Discipline.detalii este de tip XML, cu element radacina, si in exemplele prezentate, contine elemente ; exemplu: prof de inele prof de corpuri C10 - DS

5 XML - Specifies that the results of a query are to be returned as an XML document. One of the following XML modes must be specified: RAW, AUTO, EXPLICIT. RAW [ ('ElementName') ] - Takes the query result and transforms each row in the result set into an XML element with a generic identifier as the element tag. You can optionally specify a name for the row element. The resulting XML output uses the specified ElementName as the row element generated for each row. AUTO - Returns query results in a simple, nested XML tree. Each table in the FROM clause, for which at least one column is listed in the SELECT clause, is represented as an XML element. The columns listed in the SELECT clause are mapped to the appropriate element attributes. EXPLICIT - Specifies that the shape of the resulting XML tree is defined explicitly. Using this mode, queries must be written in a particular way so that additional information about the desired nesting is specified explicitly. C10 - DS

6 XMLDATA - Returns inline XDR schema, but does not add the root element to the result. If XMLDATA is specified, XDR schema is appended to the document. XMLSCHEMA [ ('TargetNameSpaceURI') ] - Returns inline XSD schema. You can optionally specify a target namespace URI when you specify this directive, which returns the specified namespace in the schema. ELEMENTS - Specifies that the columns are returned as subelements. Otherwise, they are mapped to XML attributes. This option is supported in RAW, AUTO and PATH modes only. You can optionally specify XSINIL or ABSENT when you use this directive. XSINIL specifies that an element that has an xsi:nil attribute set to True be created for NULL column values. By default or when ABSENT is specified together with ELEMENTS, no elements are created for NULL values. XSINIL - Specifies that an element with xsi:nil attribute set to True be created for NULL column values. This option can only be specified with ELEMENTS directive. C10 - DS

7 PATH [ ('ElementName') ] - Generates a element wrapper for each row in the result set. You can optionally specify an element name for the element wrapper. If an empty string is provided, such as FOR XML PATH ('') ), a wrapper element is not generated. Using PATH may provide a simpler alternative to queries written using the EXPLICIT directive. You can use FOR XML EXPLICIT mode queries to construct this kind of XML from a rowset, but the PATH mode provides a simpler alternative to the possibly cumbersome EXPLICIT mode queries. PATH mode, together with the ability to write nested FOR XML queries and the TYPE directive to return xml type instances, allows you to write queries with less complexity. It provides an alternative to writing most EXPLICIT mode queries. By default, PATH mode generates a element wrapper for each row in the result set. You can optionally specify an element name. If you do, the specified name is used as the wrapper element name. If you provide an empty string (FOR XML PATH ('')), no wrapper element is generated. C10 - DS

8 TYPE Specifies that the query returns results as xml type. ROOT [ ('RootName') ] Specifies that a single top-level element be added to the resulting XML. You can optionally specify the root element name to generate. If the optional root name is not specified, the default element is added. C10 - DS

9 Using RAW Mode –RAW mode transforms each row in the query result set into an XML element that has the generic identifier, or the optionally provided element name. –By default, each column value in the rowset that is not NULL is mapped to an attribute of the element. –If the ELEMENTS directive is added to the FOR XML clause, each column value is mapped to a subelement of the element. Together with the ELEMENTS directive, you can optionally specify the XSINIL option to map NULL column values in the result set to an element that has the attribute, xsi:nil="true". C10 - DS

10 Exemple RAW select * from studenti for xml raw => (...) C10 - DS

11 Exemple RAW select * from studenti for xml raw, elements => C10 - DS 4 110 Lupu Teodor Grigore 132 2001-01-01T00:00:00 6 152 Hodis George-Raul 821 4 154 Farcas Vasile-Sebastian 342 7.00 (...)

12 Exemple RAW select * from studenti for xml raw, elements xsinil => C10 - DS 4 110 Lupu Teodor Grigore 132 2001-01-01T00:00:00 6 152 Hodis George-Raul 821 4 154 Farcas Vasile-Sebastian 342 7.00 (...)

13 Exemple RAW select * from studenti for xml raw('student') => C10 - DS (...)

14 Exemple RAW select * from studenti for xml raw('student'), elements => C10 - DS 4 110 Lupu Teodor Grigore 132 2001-01-01T00:00:00 6 152 Hodis George-Raul 821 4 154 Farcas Vasile-Sebastian 342 7.00 (...)

15 Exemple RAW select * from studenti for xml raw('student'), root('facultate') => C10 - DS <student CodS="4" NrMatricol="110" Nume="Lupu Teodor Grigore" Grupa="132" DataN="2001-01-01T00:00:00" /> <student CodS="6" NrMatricol="152" Nume="Hodis George-Raul" Grupa="821" /> <student CodS="4" NrMatricol="154" Nume="Farcas Vasile-Sebastian" Grupa="342" Medie="7.00" /> (...)

16 Exemple RAW select CodD, DenumireD, DetaliiDisc = detalii from discipline for xml raw => C10 - DS prof de algebra 1 prof de algebra 2 prof de inele prof de corpuri (…)

17 Exemple RAW select CodD, DenumireD, DetaliiDisc = detalii from discipline for xml raw, elements => C10 - DS MA001 Structuri algebrice de baza prof de curs structuri prof de seminar structuri MA002 Algebra liniara (…)

18 Exemple RAW select se.CodS, se.DenumireS, st.NrMatricol, st.Nume from sectii as se inner join studenti as st on st.CodS = se.CodS order by se.cods for xml raw => C10 - DS (...) (...)

19 Exemple RAW select se.CodS, se.DenumireS, studenti = (select st.NrMatricol, st.Nume from studenti as st where st.CodS = se.CodS for xml raw ('student')) from sectii as se order by se.cods for xml raw, elements => C10 - DS 1 Matematica <student NrMatricol="7587" Nume="Huluban Ovidiu"/><student NrMatricol="8043" Nume="Pop Daniela Ioana"/><student NrMatricol="8070" Nume="Ulici Amalia Laura"/><student NrMatricol="8074" Nume="Vilceanu Veronica Aureli"/> (...) (...)

20 Exemple RAW select se.CodS, se.DenumireS, studenti = (select st.NrMatricol, st.Nume from studenti as st where st.CodS = se.CodS for xml raw ('student'), type) from sectii as se order by se.cods for xml raw, elements => C10 - DS 1 Matematica (...) (...)

21 Using AUTO Mode –AUTO mode returns query results as nested XML elements. This does not provide much control over the shape of the XML generated from a query result. The AUTO mode queries are useful if you want to generate simple hierarchies. –Each table in the FROM clause, from which at least one column is listed in the SELECT clause, is represented as an XML element. The columns listed in the SELECT clause are mapped to attributes or subelements, if the optional ELEMENTS option is specified in the FOR XML clause. C10 - DS

22 Using AUTO Mode –The XML hierarchy, nesting of the elements, in the resulting XML is based on the order of tables identified by the columns specified in the SELECT clause. Therefore, the order in which column names are specified in the SELECT clause is significant. The first, leftmost table that is identified forms the top element in the resulting XML document. The second leftmost table, identified by columns in the SELECT statement, forms a subelement within the top element, and so on. –If a column name listed in the SELECT clause is from a table that is already identified by a previously specified column in the SELECT clause, the column is added as an attribute of the element already created, instead of opening a new level of hierarchy. If the ELEMENTS option is specified, the column is added as an element. C10 - DS

23 Using AUTO Mode –When a column in the SELECT clause cannot be associated with any of the tables identified in the FROM clause, as in the case of an aggregate column or computed column, the column is added in the XML document in the deepest nesting level in place when it is encountered in the list. If such a column appears as the first column in the SELECT clause, the column is added to the top element. C10 - DS

24 Exemple AUTO select * from studenti for xml auto => C10 - DS (...)

25 Exemple AUTO select * from studenti for xml auto, root('facultate'), elements xsinil => C10 - DS 4 110 Lupu Teodor Grigore 132 2001-01-01T00:00:00 6 152 Hodis George-Raul 821 4 154 Farcas Vasile-Sebastian 342 7.00 (...)

26 Exemple AUTO select se.CodS, se.DenumireS, st.NrMatricol, st.Nume from sectii as se inner join studenti as st on st.CodS = se.CodS for xml auto => C10 - DS (...)

27 Exemple AUTO select se.CodS, se.DenumireS, st.NrMatricol, st.Nume from sectii as se inner join studenti as st on st.CodS = se.CodS order by se.CodS for xml auto => C10 - DS (...) (...)

28 Exemple AUTO select se.CodS, se.DenumireS, st.NrMatricol, st.Nume, r.disciplina, r.nota from sectii as se inner join studenti as st on st.CodS = se.CodS inner join rezultate as r on r.NrMatricol = st.NrMatricol order by se.cods, st.nrmatricol for xml auto, root('facultate') => C10 - DS

29 Exemple AUTO (rezultatul exemplului anterior) (...) (...) C10 - DS

30 Exemple AUTO select se.CodS, se.DenumireS, media = avg(r.nota), st.NrMatricol, st.Nume from sectii as se inner join studenti as st on st.CodS = se.CodS inner join rezultate as r on r.NrMatricol = st.NrMatricol group by se.CodS, se.DenumireS, st.NrMatricol, st.Nume order by se.cods, st.nrmatricol for xml auto, root('facultate') => C10 - DS (...)

31 Exemple AUTO select se.CodS, se.DenumireS, st.NrMatricol, st.Nume, media = avg(r.nota) from sectii as se inner join studenti as st on st.CodS = se.CodS inner join rezultate as r on r.NrMatricol = st.NrMatricol group by se.CodS, se.DenumireS, st.NrMatricol, st.Nume order by se.cods, st.nrmatricol for xml auto, root('facultate') => C10 - DS

32 Exemple AUTO (rezultatul exemplului anterior) (...) (...) C10 - DS

33 Using EXPLICIT Mode –The EXPLICIT mode query must be written in a specific way so that the additional information about the required XML, such as expected nesting in the XML, is explicitly specified as part of the query. –Because you describe the XML you want as part of the query in EXPLICIT mode, you must ensure that the generated XML is well formed and valid. C10 - DS

34 Using EXPLICIT Mode –The EXPLICIT mode transforms the rowset that results from the query execution into an XML document. In order for EXPLICIT mode to produce the XML document, the rowset must have a specific format. This requires that you write the SELECT query to produce the rowset, the universal table, with a specific format so the processing logic can then produce the XML you want. –First, the query must produce the following two metadata columns: The first column must provide the tag number, integer type, of the current element, and the column name must be Tag. Your query must provide a unique tag number for each element that will be constructed from the rowset. The second column must provide a tag number of the parent element, and this column name must be Parent. In this way, the Tag and the Parent column provide hierarchy information. C10 - DS

35 Using EXPLICIT Mode –Exemplu: universal table C10 - DS

36 Using EXPLICIT Mode –Specifying Column Names in a Universal Table –When writing EXPLICIT mode queries, column names in the resulting rowset must be specified by using this format. They provide transformation information including element and attribute names and other additional information, specified by using directives. –This is the general format: – ElementName!TagNumber!AttributeName!Directive – Following is the description of the parts of the format. –ElementName –Is the resulting generic identifier of the element. For example, if Customers is specified as ElementName, the element is generated. C10 - DS

37 Using EXPLICIT Mode –TagNumber –Is a unique tag value assigned to an element. This value, with the help of the two metadata columns, Tag and Parent, determines the nesting of the elements in the resulting XML. –AttributeName –Provides the name of the attribute to construct in the specified ElementName. This is the behavior if Directive is not specified. –If Directive is specified and it is xml, cdata, or element, this value is used to construct an element child of ElementName, and the column value is added to it. –If you specify the Directive, the AttributeName can be empty. For example, ElementName!TagNumber!!Directive. In this case, the column value is directly contained by the ElementName. C10 - DS

38 Using EXPLICIT Mode –Directive –Directive is optional. –You can use Directive to indicate how to map the string data to XML. The hide, element, elementxsinil, xml, xmltext, and cdata keywords can be used as the Directive. The hide directive hides the node. This is useful when you retrieve values only for sorting purposes, but you do not want them in the resulting XML. –The element directive generates a contained element instead of an attribute. For NULL column values, no element is generated. If you want an element generated for null column values, you can specify the elementxsinil directive. This will generate an element that has the attribute xsi:nil=TRUE. C10 - DS

39 Using EXPLICIT Mode –If the Directive and the AttributeName is not specified, for example, Customer!1, an element directive is implied, such as Customer!1!!element, and column data is contained in the ElementName. –If AttributeName is specified, the tag name is replaced by the specified name. Otherwise, the attribute is appended to the current list of attributes of the enclosing elements by putting the content at the beginning of the containment without entity encoding. The column with this directive must be a text type, such as varchar, nvarchar, char, nchar, text, or ntext. This directive can be used only with hide. C10 - DS

40 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires] from sectii => tag parent sectie!1!codsectie sectie!1!denumires ------ ----------- ------------------ -------------------------- 1 NULL 1 Matematicã 1 NULL 2 Informaticã 1 NULL 3 Matematicã-Informaticã 1 NULL 4 Matematicã economicã 1 NULL 5 Matematici aplicate 1 NULL 6 Tehnologie Informaticã C10 - DS

41 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires] from sectii for xml explicit => C10 - DS

42 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires!element] from sectii for xml explicit => Matematicã Informaticã Matematicã-Informaticã (...) C10 - DS

43 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires], null as [student!2!nrmatricol], null as [student!2!nume], null as [student!2!grupa] from sectii union all select 2 as tag, 1 as parent, cods as [sectie!1!codsectie], null as [sectie!1!denumires], nrmatricol as [student!2!nrmatricol], nume as [student!2!nume], grupa as [student!2!grupa] from studenti C10 - DS

44 Exemple EXPLICIT => C10 - DS

45 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires], null as [student!2!nrmatricol], null as [student!2!nume], null as [student!2!grupa] from sectii union all select 2 as tag, 1 as parent, cods as [sectie!1!codsectie], null as [sectie!1!denumires], nrmatricol as [student!2!nrmatricol], nume as [student!2!nume], grupa as [student!2!grupa] from studenti for xml explicit C10 - DS

46 Exemple EXPLICIT (rezultat exemplul anterior) (...) C10 - DS

47 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires], null as [student!2!nrmatricol], null as [student!2!nume], null as [student!2!grupa] from sectii union all select 2 as tag, 1 as parent, cods as [sectie!1!codsectie], null as [sectie!1!denumires], nrmatricol as [student!2!nrmatricol], nume as [student!2!nume], grupa as [student!2!grupa] from studenti order by [sectie!1!codsectie], [sectie!1!denumires] desc for xml explicit C10 - DS

48 Exemple EXPLICIT => C10 - DS

49 Exemple EXPLICIT (rezultat exemplul anterior) (...) (...) (...) C10 - DS

50 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires], null as [student!2!nrmatricol], null as [student!2!nume], null as [student!2!grupa], null as [rezultat!3!disciplina], null as [rezultat!3!nota] from sectii union all select 2 as tag, 1 as parent, cods as [sectie!1!codsectie], null as [sectie!1!denumires], nrmatricol as [student!2!nrmatricol], nume as [student!2!nume], grupa as [student!2!grupa], null as [rezultat!3!disciplina], null as [rezultat!3!nota] from studenti C10 - DS union all select 3 as tag, 2 as parent, st.cods as [sectie!1!codsectie], null as [sectie!1!denumires], r.nrmatricol as [student!2!nrmatricol], null as [student!2!nume], null as [student!2!grupa], disciplina as [rezultat!3!disciplina], nota as [rezultat!3!nota] from studenti as st inner join rezultate as r on r.nrmatricol = st.nrmatricol order by [sectie!1!codsectie], [sectie!1!denumires] desc, [student!2!nrmatricol], [student!2!nume] desc for xml explicit

51 Exemple EXPLICIT (rezultat exemplul anterior, fara FOR XML) tag parent codsectie denumires nrmatricol nume grupa disciplina nota ----- ------ --------- ----------- ---------- ----------------------- ----- ---------- ------ 1 NULL 1 Matematicã NULL NULL NULL NULL NULL 2 1 1 NULL 7587 Huluban Ovidiu 141 NULL NULL 3 2 1 NULL 7587 NULL NULL MA001 9.00 3 2 1 NULL 7587 NULL NULL MI074 8.00 2 1 1 NULL 8043 Pop Daniela Ioana 141 NULL NULL 3 2 1 NULL 8043 NULL NULL MG001 7.00 2 1 1 NULL 8070 Ulici Amalia Laura 141 NULL NULL 2 1 1 NULL 8481 Alexandru Ionel 141 NULL NULL 3 2 1 NULL 8481 NULL NULL MA001 7.00 3 2 1 NULL 8481 NULL NULL MG020 10.00 (...) 1 NULL 2 Informaticã NULL NULL NULL NULL NULL 2 1 2 NULL 7019 Damian Mircea 224 NULL NULL 3 2 2 NULL 7019 NULL NULL MA002 7.00 3 2 2 NULL 7019 NULL NULL MA001 9.00 2 1 2 NULL 7230 Cioca Nicolae Valentin 243 NULL NULL 2 1 2 NULL 7255 Iurcu Florin-Daniel 231 NULL NULL 3 2 2 NULL 7255 NULL NULL MA001 8.00 2 1 2 NULL 7674 Guzic Bogdan-Ovidiu 231 NULL NULL 2 1 2 NULL 7892 Damian Cristina Ioana 233 NULL NULL 2 1 2 NULL 7908 Ilovan Cristian Marian 232 NULL NULL 3 2 2 NULL 7908 NULL NULL MA001 10.00 C10 - DS

52 Exemple EXPLICIT (rezultat exemplul anterior, cu FOR XML) (...) (...) C10 - DS

53 Exemple EXPLICIT select 1 as tag, null as parent, cods as [sectie!1!codsectie], denumires as [sectie!1!denumires], null as [disciplina!2!codd], null as [disciplina!2!denumired], null as [student!3!nrmatricol], null as [student!3!nume] from sectii union all select 2 as tag, 1 as parent, p.cods as [sectie!1!codsectie], null as [sectie!1!denumires], d.codd as [disciplina!2!codd], d.denumired as [disciplina!2!denumired], null as [student!3!nrmatricol], null as [student!3!nume] from planinv as p inner join discipline as d on d.codd = p.codd C10 - DS union all select 3 as tag, 1 as parent, cods as [sectie!1!codsectie], null as [sectie!1!denumires], null as [disciplina!2!codd], null as [disciplina!2!denumired], nrmatricol as [student!3!nrmatricol], nume as [student!3!nume] from studenti order by [sectie!1!codsectie], [sectie!1!denumires] desc, [disciplina!2!codd] desc, [student!3!nrmatricol] for xml explicit

54 C10 - DS

55 Exemple EXPLICIT (rezultat exemplul anterior, cu FOR XML) (...) (...) (...) (...) C10 - DS

56 Using PATH Mode –PATH mode, together with the ability to write nested FOR XML queries and the TYPE directive to return xml type instances, allows you to write queries with less complexity. –In PATH mode, column names or column aliases are treated as XPath expressions. These expressions indicate how the values are being mapped to XML. Each XPath expression is a relative XPath that provides the item type., such as the attribute, element, and scalar value, and the name and hierarchy of the node that will be generated relative to the row element. C10 - DS

57 Using PATH Mode –Columns without a Name –Any column without a name will be inlined. For example, computed columns or nested scalar queries that do not specify column alias will generate columns without any name. If the column is of xml type, the content of that data type instance is inserted. Otherwise, the column content is inserted as a text node. SELECT 2+2 FOR XML PATH –Produce this XML. By default, for each row in the rowset, a element is generated in the resulting XML. This is the same as RAW mode. 4 –The following query returns a three-column rowset. The third column without a name has XML data. The PATH mode inserts an instance of the xml type. C10 - DS

58 Using PATH Mode –Columns with a Name –Following are the specific conditions in which rowset columns with a name are mapped, case-sensitive, to the resulting XML: –Column name starts with an at sign (@) –Column name does not start with an at sign (@) –Column name does not start with an at sign@ and contains a slash mark (/) –Several columns share the same prefix –One column has a different name C10 - DS

59 Exemple PATH select * from studenti for xml path => C10 - DS 4 110 Lupu Teodor Grigore 132 2001-01-01T00:00:00 6 152 Hodis George-Raul 821 4 154 Farcas Vasile-Sebastian 342 7.00 (...)

60 Exemple PATH select NrMatricol as '@NrMatr', CodS as '@CodS', Nume, Grupa from studenti for xml path('student') => C10 - DS Lupu Teodor Grigore 132 Maguran Florin-Ciprian 132 Milasan Cristian Dorel 132 (...)

61 Exemple PATH select NrMatricol as '@NrMatr', CodS as '@CodS', Nume, Grupa as 'detalii/grupa', datan as 'detalii/datanasterii' from studenti for xml path('student') => C10 - DS Lupu Teodor Grigore 132 2001-01-01T00:00:00 Maguran Florin-Ciprian 132 2006-02-01T00:00:00 (...)

62 Exemple PATH select st.nrmatricol, st.nume, r.disciplina, r.nota from studenti as st inner join rezultate as r on r.nrmatricol = st.nrmatricol for xml path('student') => C10 - DS 154 Farcas Vasile-Sebastian MA001 7.00 525 Anghelcev Daniel MO001 8.00 525 Anghelcev Daniel MI006 9.00 (...)

63 Exemple PATH select st.nrmatricol, st.nume, (select disciplina, nota from rezultate as r where r.nrmatricol = st.nrmatricol for xml path('rezultat'), type) as note from studenti as st for xml path('student') => C10 - DS 110 Lupu Teodor Grigore (...) 154 Farcas Vasile-Sebastian MA001 7.00 (...) 525 Anghelcev Daniel MO001 8.00 MI006 9.00

64 Exemple PATH select nrmatricol, cods as 'data()', nume as '*', grupa as 'detalii/grupa' from studenti for xml path('student') => 110 4Lupu Teodor Grigore 132 120 4Maguran Florin-Ciprian 132 (...) C10 - DS

65 Exemple PATH select codd as "data()" from discipline for xml path ('') => MA001 MA002 MA003 MA004 MA005 MG001 MG002 MG003 MG020 MI001 MI003 MI004 MI006 MI011 MI039 MI074 MI081 MI082 MO001 MO002 MO030 C10 - DS

66 Exemple PATH select st.nrmatricol, st.nume, (select disciplina as "data()" from rezultate as r where r.nrmatricol = st.nrmatricol for xml path('')) as note from studenti as st for xml path => 110 Lupu Teodor Grigore (...) 154 Farcas Vasile-Sebastian MA001 (...) 525 Anghelcev Daniel MO001 MI006 (...) C10 - DS

67 Next 11 –Oracle - OR C10 - DS


Download ppt "C10. SQL-S + XML Date Semistructurate, 2012-2013."

Similar presentations


Ads by Google