XML Kurzanleitung
XML kurz gefasst für Programmier-Profis aus anderen Bereichen, die sich einen Überblick verschaffen wollen.
XML-Struktur
<grossvater> <vater> <ich attribut="Wert des Attributkontens"> <kind>Wert des 1. Elementknotens vom Typ "kind"</kind> <kind>Wert</kind> </ich> <geschwister/> <geschwister/> </vater> </grossvater>
XPath
XPath adressiert Pfade in einem XML-Dokument.
Pfade
Ausdruck | Beschreibung |
---|---|
knotenname | Wähle alle Kindknoten des Elementknotens knotenname |
/ | Wähle die Dokumentwurzel (wenn ein Pfad mit / startet, ist er absolut adressiert) |
vatername/kindkname | Wähle alle Elementknoten kindname unter vatername |
// | Wähle Elementknoten in jeder Tiefe im Dokument, ausgehend vom aktuellen, die auf die Auswahl passen |
. | Wähle den aktuellen Knoten |
self:: | Wähle den aktuellen Knoten |
.. | Wähle den Übergeordneten Knoten |
@attributname | Wähle Attributknoten mit dem Namen attributname |
* | Wähle jeden beliebigen Elementknoten |
@* | Wähle jeden beliebigen Attributknoten |
node() | Wähle jeden beliebigen Knoten, Element oder Attribute |
knoten1|knoten2 | Wähle jeden Elementknoten vom Typ knoten1 und knoten2 |
ancestor:: | Wähle alle Vorfahren in beliebiger Tiefe zum aktuellen Element |
ancestor-or-self:: | Wähle alle Vorfahren in beliebiger Tiefe zum aktuellen Element und das Element selbst |
attribute:: | Wähle alle Attribute des Elements |
child:: | Wähle alle Kinder des Elements |
descendant:: | Wähle alle Nachkommen des Elements |
descendant-or-self:: | Wähle alle Nachkommen des Elements und das Element selbst |
following:: | Wähle alles, was nach dem Schliessenden Tag des aktuellen Elements folgt |
following-sibling:: | Wähle alle nachfolgenden Geschwister |
namespace:: | Wähle alle Namensraumknoten des aktuellen Elements |
parent:: | Wähle den Vater des aktuellen Elements |
preceding:: | Wähle alles, was vor dem aktuellen Elements kommt |
preceding-sibling:: | Wähle alle Geschwister vor dem aktuellen Element |
Eigenschaften, Bedingungen
Eigenschaft | Beschreibung |
---|---|
[n] | Wähle das n-te Element, z.B. vater/kind[1] wählt den ersten1) Knoten namens kind unter dem Knoten vater |
[kind=wert] | Wähle alle Elemente, deren Kind kind den Wert wert haben |
[@attr] | Wähle alle Elemente, die das Attribut attr haben |
[@attr="wert"] | Wähle alle Elemente, die das Attribut attr mit Wert wert haben |
Es sind auch Ausdrücke möglich, wie last()-1
, position()<3
, u.s.w., z.B. in einem Buchladen alle Titel wählen von Büchern, die mehr als 35,00 kosten: /bookstore/book[price>35.00]/title
.
Operatoren
Operator | Funktion |
---|---|
+ | Addition |
- | Subtraktion |
* | Multiplikation |
div | Division |
= | Gleichheit |
!= | Ungleichheit |
< | Kleiner als |
<= | Kleiner oder gleich |
> | Grösser als |
>= | Grösser oder gleich |
or | Oder |
and | Und |
mod | Modulus |
XQuery
XQuery-Abfragen kann man z.B. mit xqilla
ausführen (sudo apt-get install xqilla
). Auf Pfade kann man mit XPath zugreifen, z.B. liefert doc("index.html")/html/body/h1
alle Titel der ersten Stufe einer HTML-Seite.
Syntax
XQuery besteht aus XPath Abfragen oder speziellen FLWOR Konstrukten aus «For, Let, Where, Order by, Return».
Wenn ein XML-Tag ausgegeben werden soll, schreibt man einfach das XML Tag, muss dann aber XQuery in geschweifte Klammern verpacken:
Beispiel (zusammenführen von alten Übersetzungen in eine neu generierte Qt-TS-Datei):
for $y in doc("translation.new")/TS/context order by $y/name return <context> {$y/name} { for $x in $y/message order by $x/source return <message> {$x/location} {$x/source} {doc("translation.old")/TS/context[name=$y/name]/message[source=$x/source]/translation} </message> } </context>
Kontrollfluss
FLWOR ist eine Abkürzung von "For, Let, Where, Order by, Return".
Listen: (1 to 5)
oder (1, 2, 3, 4, 5)
Vergleiche allgemein: =, !=, <, <=, >, >= Vergleiche nach Wert: eq, ne, lt, le, gt, ge
Kommando | Wirkung |
---|---|
for $x in pfad/zum/element for $x in (10,20), $y in (100,200) | Iteriere die Variabel x (und y) durch alle passenden Elemente (und Werte) |
order by sortiertag | Sortiere die for -Schlaufe nac dem Wert von sorttiertag |
where bedingung | Beschränke die for -Schlaufe auf Werte, die der Bedingung entsprechen |
return elemente | Elemente, die ausgegeben werden |
if (bedingung) then elemente else elemente | Bedingte Rückgabe von Elementen |
let $x := (1 to 5) | Wert zuweisen an x, hier 1 2 3 4 5 |
Funktionen
declare function prefix:function_name($parameter AS datatype) AS returnDatatype { ...function code here... }
Type is specified as XML Schema.
XML-Schema
Beispiel XML-Schema:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Beispiel Verwendung von XML-Schema:
<?xml version="1.0"?> <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Typen:
Name | Funktion |
---|---|
Funktionen
Standardfunktionen haben den Präfix fn:
, den man auch weglassen kann. fn:doc
ist dasselbe, wie doc
.
Name | Description |
---|---|
Accessor Functions | |
fn:node-name(node) | Returns the node-name of the argument node |
fn:nilled(node) | Returns a Boolean value indicating whether the argument node is nilled |
fn:data(item.item,…) | Takes a sequence of items and returns a sequence of atomic values |
fn:base-uri() fn:base-uri(node) | Returns the value of the base-uri property of the current or specified node |
fn:document-uri(node) | Returns the value of the document-uri property for the specified node |
Error and Trace Functions | |
fn:error() fn:error(error) fn:error(error,description) fn:error(error,description,error-object) | Example: error(fn:QName('http://example.com/test', 'err:toohigh'), 'Error: Price is too high') Result: Returns http://example.com/test#toohigh and the string "Error: Price is too high" to the external processing environment |
fn:trace(value,label) | Used to debug queries |
Functions on Numeric Values | |
fn:number(arg) | Returns the numeric value of the argument. The argument could be a boolean, string, or node-set |
fn:abs(num) | Returns the absolute value of the argument |
fn:ceiling(num) | Returns the smallest integer that is greater than the number argument |
fn:floor(num) | Returns the largest integer that is not greater than the number argument |
fn:round(num) | Rounds the number argument to the nearest integer |
fn:round-half-to-even() | Example: round-half-to-even(0.5) |
Functions on Strings | |
fn:string(arg) | Returns the string value of the argument. The argument could be a number, boolean, or node-set |
fn:codepoints-to-string(int,int,…) | Returns a string from a sequence of code points |
fn:string-to-codepoints(string) | Returns a sequence of code points from a string |
fn:codepoint-equal(comp1,comp2) | Returns true if the value of comp1 is equal to the value of comp2, according to the Unicode code point collation (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise it returns false |
fn:compare(comp1,comp2) fn:compare(comp1,comp2,collation) | Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 (according to the rules of the collation that is used) |
fn:concat(string,string,…) | Returns the concatenation of the strings |
fn:string-join((string,string,…),sep) | Returns a string created by concatenating the string arguments and using the sep argument as the separator |
fn:substring(string,start,len) fn:substring(string,start) | Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end |
fn:string-length(string) fn:string-length() | Returns the length of the specified string. If there is no string argument it returns the length of the string value of the current node |
fn:normalize-space(string) fn:normalize-space() | Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node |
fn:normalize-unicode() | |
fn:upper-case(string) | Converts the string argument to upper-case |
fn:lower-case(string) | Converts the string argument to lower-case |
fn:translate(string1,string2,string3) | Converts string1 by replacing the characters in string2 with the characters in string3 |
fn:escape-uri(stringURI,esc-res) | Example: escape-uri("http://example.com/test#car", true()) |
fn:contains(string1,string2) | Returns true if string1 contains string2, otherwise it returns false |
fn:starts-with(string1,string2) | Returns true if string1 starts with string2, otherwise it returns false |
fn:ends-with(string1,string2) | Returns true if string1 ends with string2, otherwise it returns false |
fn:substring-before(string1,string2) | Returns the start of string1 before string2 occurs in it |
fn:substring-after(string1,string2) | Returns the remainder of string1 after string2 occurs in it |
fn:matches(string,pattern) | Returns true if the string argument matches the pattern, otherwise, it returns false |
fn:replace(string,pattern,replace) | Returns a string that is created by replacing the given pattern with the replace argument |
fn:tokenize(string,pattern) | Example: tokenize("XPath is fun", "\s+") |
Functions for anyURI | |
fn:resolve-uri(relative,base) | |
Functions on Boolean Values | |
fn:boolean(arg) | Returns a boolean value for a number, string, or node-set |
fn:not(arg) | The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true |
fn:true() | Returns the boolean value true |
fn:false() | Returns the boolean value false |
Functions on Durations, Dates and Times | |
Component Extraction Functions on Durations, Dates and Times | |
fn:dateTime(date,time) | Converts the arguments to a date and a time |
fn:years-from-duration(datetimedur) | Returns an integer that represents the years component in the canonical lexical representation of the value of the argument |
fn:months-from-duration(datetimedur) | Returns an integer that represents the months component in the canonical lexical representation of the value of the argument |
fn:days-from-duration(datetimedur) | Returns an integer that represents the days component in the canonical lexical representation of the value of the argument |
fn:hours-from-duration(datetimedur) | Returns an integer that represents the hours component in the canonical lexical representation of the value of the argument |
fn:minutes-from-duration(datetimedur) | Returns an integer that represents the minutes component in the canonical lexical representation of the value of the argument |
fn:seconds-from-duration(datetimedur) | Returns a decimal that represents the seconds component in the canonical lexical representation of the value of the argument |
fn:year-from-dateTime(datetime) | Returns an integer that represents the year component in the localized value of the argument |
fn:month-from-dateTime(datetime) | Returns an integer that represents the month component in the localized value of the argument |
fn:day-from-dateTime(datetime) | Returns an integer that represents the day component in the localized value of the argument |
fn:hours-from-dateTime(datetime) | Returns an integer that represents the hours component in the localized value of the argument |
fn:minutes-from-dateTime(datetime) | Returns an integer that represents the minutes component in the localized value of the argument |
fn:seconds-from-dateTime(datetime) | Returns a decimal that represents the seconds component in the localized value of the argument |
fn:timezone-from-dateTime(datetime) | Returns the time zone component of the argument if any |
fn:year-from-date(date) | Returns an integer that represents the year in the localized value of the argument |
fn:month-from-date(date) | Returns an integer that represents the month in the localized value of the argument |
fn:day-from-date(date) | Returns an integer that represents the day in the localized value of the argument |
fn:timezone-from-date(date) | Returns the time zone component of the argument if any |
fn:hours-from-time(time) | Returns an integer that represents the hours component in the localized value of the argument |
fn:minutes-from-time(time) | Returns an integer that represents the minutes component in the localized value of the argument |
fn:seconds-from-time(time) | Returns an integer that represents the seconds component in the localized value of the argument |
fn:timezone-from-time(time) | Returns the time zone component of the argument if any |
fn:adjust-dateTime-to-timezone(datetime,timezone) | If the timezone argument is empty, it returns a dateTime without a timezone. Otherwise, it returns a dateTime with a timezone |
fn:adjust-date-to-timezone(date,timezone) | If the timezone argument is empty, it returns a date without a timezone. Otherwise, it returns a date with a timezone |
fn:adjust-time-to-timezone(time,timezone) | If the timezone argument is empty, it returns a time without a timezone. Otherwise, it returns a time with a timezone |
Functions Related to QNames | |
fn:QName() | |
fn:local-name-from-QName() | |
fn:namespace-uri-from-QName() | |
fn:namespace-uri-for-prefix() | |
fn:in-scope-prefixes() | |
fn:resolve-QName() | |
Functions on Nodes | |
fn:name() fn:name(nodeset) | Returns the name of the current node or the first node in the specified node set |
fn:local-name() fn:local-name(nodeset) | Returns the name of the current node or the first node in the specified node set - without the namespace prefix |
fn:namespace-uri() fn:namespace-uri(nodeset) | Returns the namespace URI of the current node or the first node in the specified node set |
fn:lang(lang) | Returns true if the language of the current node matches the language of the specified language |
fn:root() fn:root(node) | Returns the root of the tree to which the current node or the specified belongs. This will usually be a document node |
Functions on Sequences | |
General Functions on Sequences | |
fn:index-of((item,item,…),searchitem) | Returns the positions within the sequence of items that are equal to the searchitem argument |
fn:remove((item,item,…),position) | Returns a new sequence constructed from the value of the item arguments - with the item specified by the position argument removed |
fn:empty(item,item,…) | Returns true if the value of the arguments IS an empty sequence, otherwise it returns false |
fn:exists(item,item,…) | Returns true if the value of the arguments IS NOT an empty sequence, otherwise it returns false |
fn:distinct-values((item,item,…),collation) | Returns only distinct (different) values |
fn:insert-before((item,item,…),pos,inserts) | Returns a new sequence constructed from the value of the item arguments - with the value of the inserts argument inserted in the position specified by the pos argument |
fn:reverse((item,item,…)) | Returns the reversed order of the items specified |
fn:subsequence((item,item,…),start,len) | Returns a sequence of items from the position specified by the start argument and continuing for the number of items specified by the len argument. The first item is located at position 1 |
fn:unordered((item,item,…)) | Returns the items in an implementation dependent order |
Functions That Test the Cardinality of Sequences | |
fn:zero-or-one(item,item,…) | Returns the argument if it contains zero or one items, otherwise it raises an error |
fn:one-or-more(item,item,…) | Returns the argument if it contains one or more items, otherwise it raises an error |
fn:exactly-one(item,item,…) | Returns the argument if it contains exactly one item, otherwise it raises an error |
Equals, Union, Intersection and Except | |
fn:deep-equal(param1,param2,collation) | Returns true if param1 and param2 are deep-equal to each other, otherwise it returns false |
Aggregate Functions | |
fn:count((item,item,…)) | Returns the count of nodes |
fn:avg((arg,arg,…)) | Returns the average of the argument values |
fn:max((arg,arg,…)) | Returns the argument that is greater than the others |
fn:min((arg,arg,…)) | Returns the argument that is less than the others |
fn:sum(arg,arg,…) | Returns the sum of the numeric value of each node in the specified node-set |
Functions that Generate Sequences | |
fn:id((string,string,…),node) | Returns a sequence of element nodes that have an ID value equal to the value of one or more of the values specified in the string argument |
fn:idref((string,string,…),node) | Returns a sequence of element or attribute nodes that have an IDREF value equal to the value of one or more of the values specified in the string argument |
fn:doc(URI) | Einlesen einer XML-Datei |
fn:doc-available(URI) | Returns true if the doc() function returns a document node, otherwise it returns false |
fn:collection() fn:collection(string) | |
Context Functions | |
fn:position() | Returns the index position of the node that is currently being processed |
fn:last() | Returns the number of items in the processed node list |
fn:current-dateTime() | Returns the current dateTime (with timezone) |
fn:current-date() | Returns the current date (with timezone) |
fn:current-time() | Returns the current time (with timezone) |
fn:implicit-timezone() | Returns the value of the implicit timezone |
fn:default-collation() | Returns the value of the default collation |
fn:static-base-uri() | Returns the value of the base-uri |
0
statt 1