Tecnologías XML / XSLT / Recetario XSLT / Consultar XML / Implementar casos XML Query de W3C en XSLT.
(Extracto de «XSLT Kochbuch» de Sal Mangano)
Problema
Necesita una consulta similar a un caso de XML Query Use Cases pero quiere XSLT en vez de XQuery.
Solución
Estos ejemplos resuelven en XSLT la mayoría de casos W3C. Sus descripciones siguen de cerca ese documento.
Caso «XMP»: Experiencias y ejemplos.
Las consultas ilustran requisitos recogidos de comunidades de bases y documentos. Los cuatro ejemplos siguientes dan sus datos.
Ejemplo: bib.xml
<bib>
<book year="1994">
<title>TCP/IP Illustrated</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price> 65.95</price>
</book>
<book year="1992">
<title>Advanced Programming in the Unix environment</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="2000">
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
<publisher>Morgan Kaufmann Publishers</publisher>
<price> 39.95</price>
</book>
<book year="1999">
<title>The Economics of Technology and Content for Digital TV</title>
<editor>
<last>Gerbarg</last><first>Darcy</first>
<affiliation>CITI</affiliation>
</editor>
<publisher>Kluwer Academic Publishers</publisher>
<price>129.95</price>
</book>
</bib>Ejemplo: reviews.xml
<reviews>
<entry>
<title>Data on the Web</title>
<price>34.95</price>
<review>A very good discussion of semi-structured database systems and XML.</review>
</entry>
<entry>
<title>Advanced Programming in the Unix environment</title>
<price>65.95</price>
<review>A clear and detailed discussion of UNIX programming.</review>
</entry>
<entry>
<title>TCP/IP Illustrated</title>
<price>65.95</price>
<review>One of the best books on TCP/IP.</review>
</entry>
</reviews>Ejemplo: books.xml
<chapter>
<title>Data Model</title>
<section>
<title>Syntax For Data Model</title>
</section>
<section>
<title>XML</title>
<section>
<title>Basic Syntax</title>
</section>
<section>
<title>XML and Semistructured Data</title>
</section>
</section>
</chapter>Ejemplo: prices.xml
<prices>
<book>
<title>Advanced Programming in the Unix environment</title>
<source>www.amazon.com</source>
<price>65.95</price>
</book>
<book>
<title>Advanced Programming in the Unix environment</title>
<source>www.bn.com</source>
<price>65.95</price>
</book>
<book>
<title>TCP/IP Illustrated</title>
<source>www.amazon.com</source>
<price>65.95</price>
</book>
<book>
<title>TCP/IP Illustrated</title>
<source>www.bn.com</source>
<price>65.95</price>
</book>
<book>
<title>Data on the Web</title>
<source>www.amazon.com</source>
<price>34.95</price>
</book>
<book>
<title>Data on the Web</title>
<source>www.bn.com</source>
<price>39.95</price>
</book>
</prices><!-- Frage 1. Liste Bücher in bib.xml auf, die von Addison-Wesley nach 1991 veröffentlicht wurden, einschließlich ihres Jahres und Titels: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:template match="book[publisher = 'Addison-Wesley' and @year > 1991]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="book"/>
</xsl:stylesheet>
<!-- Frage 2. Erzeuge eine einfache Liste aller Titel-Autor-Paare aus bib.xml, wobei jedes Paar in ein "result"-Element eingeschlossen ist: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<results>
<xsl:apply-templates select="bib/book/author"/>
</results>
</xsl:template>
<xsl:template match="author">
<result>
<xsl:copy-of select="preceding-sibling::title"/>
<xsl:copy-of select="."/>
</result>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 3. Liste für jedes Buch in bib.xml Titel und Autoren auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bib">
<results>
<xsl:for-each select="book">
<result>
<xsl:copy-of select="title"/>
<xsl:copy-of select="author"/>
</result>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 4. Liste für jeden Autor in bib.xml den Namen des Autors und die Titel aller Bücher dieses Autors auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<results>
<xsl:for-each select="//author[not(.=preceding::author)]">
<result>
<xsl:copy-of select="."/>
<xsl:for-each select="/bib/book[author=current()]">
<xsl:copy-of select="title"/>
</xsl:for-each>
</result>
</xsl:for-each>
</results>
<!-- Frage 5. Liste für alle Bücher, die sowohl auf http://www.bn.com (bib.xml) als auch auf http://www.amazon.com (reviews.xml) gefunden wurden, den Titel des Buches und den Preis bei der jeweiligen Quelle auf: -->
<xsl:variable name="bn" select="document('bib.xml')"/>
<xsl:variable name="amazon" select="document('reviews.xml')"/>
</xsl:template>
</xsl:stylesheet>
<!--Lösung 1 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<books-with-prices>
<xsl:for-each select="$bn//book[title = $amazon//entry/title]">
<book-with-prices>
<xsl:copy-of select="title"/>
<price-amazon>
<xsl:value-of select="$amazon//entry[title=current()/title]/price"/>
</price-amazon>
<price-bn>
<xsl:value-of select="price"/>
</price-bn>
</book-with-prices>
</xsl:for-each>
</books-with-prices>
</xsl:template>
</xsl:stylesheet>
<!--Lösung 2-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<books-with-prices>
<xsl:for-each select="$bn//book">
<xsl:variable name="bn-book" select="."/>
<xsl:for-each select="$amazon//entry[title=$bn-book/title]">
<book-with-prices>
<xsl:copy-of select="title"/>
<price-amazon>
<xsl:value-of select="price"/>
</price-amazon>
<price-bn>
<xsl:value-of select="$bn-book/price"/>
</price-bn>
</book-with-prices>
</xsl:for-each>
</xsl:for-each>
</books-with-prices>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 6. Liste für jedes Buch, das mindestens einen Autor hat, den Titel und die ersten beiden Autoren sowie ein leeres "et-al"-Element auf, wenn das Buch weitere Autoren hat: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bib">
<xsl:copy>
<xsl:for-each select="book[author]">
<xsl:copy>
<xsl:copy-of select="title"/>
<xsl:copy-of select="author[position() <= 2]"/>
<xsl:if test="author[3]">
<et-al/>
</xsl:if>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 7. Liste in alphabetischer Reihenfolge die Titel und Jahre aller Bücher auf, die nach 1991 von Addison-Wesley veröffentlicht wurden: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bib">
<xsl:copy>
<xsl:for-each select="book[publisher = 'Addison-Wesley' and @year > 1991]">
<xsl:sort select="title"/>
<xsl:copy>
<xsl:copy-of select="@year"/>
<xsl:copy-of select="title"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 8. Suche im Dokument books.xml alle Abschnitts-(section-) oder Kapitel-(chapter-)titel, die das Wort "XML" enthalten, ungeachtet der Verschachtelungsebene: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<results>
<xsl:copy-of select="(//chapter/title | //section/title)[contains(.,'XML')]"/>
</results>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 9. Suche im Dokument prices.xml den Minimalpreis für jedes Buch in Form eines "minprice"-Elements mit dem Buchtitel als Titelattribut: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="../math/math.min.xslt"/>
<xsl:template match="/">
<results>
<xsl:for-each select="//book/title[not(. = ./preceding::title)]">
<xsl:variable name="min-price">
<xsl:call-template name="math:min">
<xsl:with-param name="nodes" select="//book[title = current()]/price"/>
</xsl:call-template>
</xsl:variable>
<minprice title="{.}">
<price>
<xsl:value-of select="$min-price"/>
</price>
</minprice>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 10. Liefere für jedes Buch mit einem Autor das Buch mit seinem Titel und seinen Autoren zurück. Liefere für jedes Buch mit einem Herausgeber eine Referenz mit dem Buchtitel und der Zugehörigkeit des Herausgebers zurück: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bib">
<xsl:copy>
<xsl:for-each select="book[author]">
<xsl:copy>
<xsl:copy-of select="title"/>
<xsl:copy-of select="author"/>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="book[editor]">
<reference>
<xsl:copy-of select="title"/>
<org><xsl:value-of select="editor/affiliation"/></org>
</reference>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<!-- Frage 11. Suche Paare von Büchern, die unterschiedliche Titel, aber die gleichen Autoren (möglicherweise in anderer Reihenfolge) haben: -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="query.equal-values.xslt"/>
<xsl:template match="bib">
<xsl:copy>
<xsl:for-each select="book[author]">
<xsl:variable name="book1" select="."/>
<xsl:for-each select="./following-sibling::book[author]">
<xsl:variable name="same-authors">
<xsl:call-template name="query:equal-values">
<xsl:with-param name="nodes1" select="$book1/author"/>
<xsl:with-param name="nodes2" select="author"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="string($same-authors)">
<book-pair>
<xsl:copy-of select="$book1/title"/>
<xsl:copy-of select="title"/>
</book-pair>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>Caso «TREE»: Conservar jerarquía.
Algunos tipos XML mezclan texto y elementos opcionales en estructuras variables donde importan orden y anidación. Una consulta debe extraer elementos conservando esa estructura. Este caso usa un tipo flexible llamadoBook.La DTD y la entrada XML siguen en dos ejemplos.
Ejemplo: book.dtd
<!ELEMENT book (title, author+, section+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT section (title, (p | figure | section)* )>
<!ATTLIST section
id ID #IMPLIED
difficulty CDATA #IMPLIED>
<!ELEMENT p (#PCDATA)>
<!ELEMENT figure (title, image)>
<!ATTLIST figure
width CDATA #REQUIRED
height CDATA #REQUIRED >
<!ELEMENT image EMPTY>
<!ATTLIST image
source CDATA #REQUIRED >Ejemplo: book.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book SYSTEM "book.dtd">
<book>
<title>Data on the Web</title>
<author>Serge Abiteboul</author>
<author>Peter Buneman</author>
<author>Dan Suciu</author>
<section id="intro" difficulty="easy">
<title>Introduction</title>
<p>Text ... </p>
<section>
<title>Audience</title>
<p>Text ... </p>
</section>
<section>
<title>Web Data and the Two Cultures</title>
<p>Text ... </p>
<figure height="400" width="400">
<title>Traditional client/server architecture</title>
<image source="csarch.gif"/>
</figure>
<p>Text ... </p>
</section>
</section>
<section id="syntax" difficulty="medium" >
<title>A Syntax For Data</title>
<p>Text ... </p>
<figure height="200" width="500">
<title>Graph representations of structures</title>
<image source="graphs.gif"/>
</figure>
<p>Text ... </p>
<section>
<title>Base Types</title>
<p>Text ... </p>
</section>
<section>
<title>Representing Relational Databases</title>
<p>Text ... </p>
<figure height="250" width="400">
<title>Examples of Relations</title>
<image source="relations.gif"/>
</figure>
</section>
<section>
<title>Representing Object Databases</title>
<p>Text ... </p>
</section>
</section>
</book><!-- Frage 1. Bereite ein (geschachteltes) Inhaltsverzeichnis für Book1 vor, in dem alle Abschnitte und deren Titel aufgelistet werden. Die Originalattribute der einzelnen <section>-Elemente sollen beibehalten werden, falls welche existieren: -->
<xsl:template match="book">
<toc>
<xsl:apply-templates/>
</toc>
</xsl:template>
<!-- Kopieren des Elements von toc -->
<xsl:template match="section | section/title | section/title/text()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- Unterdrücken anderer Elemente -->
<xsl:template match="* | text()"/>
<!-- Frage 2. Bereite eine (einfache) Abbildungsliste für Book1 vor, in der alle Abbildungen und deren Titel aufgelistet werden. Die Originalattribute der einzelnen <figure>-Elemente sollen beibehalten werden, falls welche existieren: -->
<xsl:template match="book">
<figlist>
<xsl:for-each select=".//figure">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="title"/>
</xsl:copy>
</xsl:for-each>
</figlist>
</xsl:template>
<!-- Frage 3. Wie viele Abschnitte (section) und wie viele Abbildungen (figure) sind in Book1? -->
<xsl:template match="/">
<section-count><xsl:value-of select="count(//section)"/></section-count>
<figure-count><xsl:value-of select="count(//figure)"/></figure-count>
</xsl:template>
<!-- Frage 4. Wie viele Hauptabschnitte gibt es in Book1? -->
<xsl:template match="book">
<top_section_count>
<xsl:value-of select="count(section)"/>
</top_section_count>
</xsl:template>
<!-- Frage 5. Erzeuge eine einfache Liste der section-Elemente in Book1. Anstelle seiner Originalattribute soll jedes section-Element zwei Attribute haben, die den Titel des Abschnitts und die Anzahl der Abbildungen enthalten, die sich unmittelbar im Abschnitt befinden: -->
<xsl:template match="book">
<section_list>
<xsl:for-each select=".//section">
<section title="{title}" figcount="{count(figure)}"/>
</xsl:for-each>
</section_list>
</xsl:template>
<!-- Frage 6. Erzeuge eine geschachtelte Liste der section-Elemente in Book1, wobei ihre Originalattribute und die ursprüngliche Hierarchie erhalten bleiben. Füge in jedem section-Element den Titel des Abschnitts sowie ein Element hinzu, das die Anzahl der Abbildungen enthält, die sich unmittelbar im Abschnitt befinden (siehe Beispiel "Die Lösung, so wie ich die englischen Anforderungen interpretieren würde" und Beispiel "Was der W3C-Anwendungsfall, basierend auf einem Beispielergebnis und XQuery, haben möchte"). -->Ejemplo: Mi interpretación de los requisitos ingleses.
<xsl:template match="book">
<toc>
<xsl:apply-templates select="section"/>
</toc>
</xsl:template>
<xsl:template match="section">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="title"/>
<figcount><xsl:value-of select="count(figure)"/></figcount>
<xsl:apply-templates select="section"/>
</xsl:copy>
</xsl:template>Ejemplo: La intención W3C deducida de la salida y XQuery.
<xsl:template match="book">
<toc>
<xsl:for-each select="//section">
<xsl:sort select="count(ancestor::section)"/>
<xsl:apply-templates select="."/>
</xsl:for-each>
</toc>
</xsl:template>
<xsl:template match="section">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="title"/>
<figcount><xsl:value-of select="count(figure)"/></figcount>
<xsl:apply-templates select="section"/>
</xsl:copy>
</xsl:template>Caso «SEQ»: Consultas basadas en secuencia.
Las consultas usan el orden de elementos. Menos importante en bases tradicionales, la secuencia puede ser esencial en documentos estructurados. Este caso consulta un informe médico:
<!DOCTYPE report [
<!ELEMENT report (section*)>
<!ELEMENT section (section.title, section.content)>
<!ELEMENT section.title (#PCDATA )>
<!ELEMENT section.content (#PCDATA | anesthesia | prep | incision | action | observation )*>
<!ELEMENT anesthesia (#PCDATA)>
<!ELEMENT prep ( (#PCDATA | action)* )>
<!ELEMENT incision ( (#PCDATA | geography | instrument)* )>
<!ELEMENT action ( (#PCDATA | instrument )* )>
<!ELEMENT observation (#PCDATA)>
<!ELEMENT geography (#PCDATA)>
<!ELEMENT instrument (#PCDATA)>
]>
<report>
<section>
<section.title>Procedure</section.title>
<section.content>The patient was taken to the operating room where she was placed in supine position and <anesthesia>induced under general anesthesia.</anesthesia> <prep><action>A Foley catheter was placed to decompress the bladder</action> and the abdomen was then prepped and draped in sterile fashion.</prep> <incision>A curvilinear incision was made <geography>in the midline immediately infraumbilical</geography> and the subcutaneous issue was divided <instrument>using electrocautery.</instrument></incision> The fascia was identified and <action>#2 0 Maxon stay sutures were placed on each side of the midline.</action> <incision>The fascia was divided using <instrument>electrocautery</instrument> and the peritoneum was entered.</incision> <observation>The small bowel was identified.</observation> and <action> the <instrument>Hasson trocar</instrument> was placed under direct visualization.</action> <action>The <instrument>trocar</instrument> was secured to the fascia using the stay sutures.</action></section.content>
</section>
</report>
<!-- Frage 1. Welche Instrumente wurden im Abschnitt Procedure von Report1 beim zweiten Einschnitt (incision) verwendet? -->
<xsl:template match="section[section.title = 'Procedure']">
<xsl:copy-of select="(.//incision)[2]/instrument"/>
</xsl:template>
<!-- Frage 2. Welches sind die ersten beiden Instrumente, die im Abschnitt Procedure von Report1 zum Einsatz kamen? -->
<xsl:template match="section[section.title = 'Procedure']">
<xsl:copy-of select="(.//instrument)[position( ) <= 2]"/>
</xsl:template>
<!-- Frage 3. Welche Instrumente wurden in Report1 bei den ersten beiden Aktionen nach dem zweiten Einschnitt verwendet? -->
<xsl:template match="report">
<!-- i2 = Zweiter Einschnitt im gesamten Bericht -->
<xsl:variable name="i2" select="(.//incision)[2]"/>
<!-- Beziehe die Instrumente, die in den ersten beiden Aktionen verwendet werden, die auf i2 folgen -->
<xsl:copy-of select="($i2/following::action)[position( ) <= 2]/instrument"/>
</xsl:template>
<!-- Frage 4. Suche in Report1 "Procedure"-Abschnitte, in denen vor dem ersten Einschnitt (incision) kein anesthesia-Element auftaucht: -->
<xsl:template match="section[section.title = 'Procedure']">
<xsl:variable name="i1" select="(.//incision)[1]"/>
<xsl:if test=".//anesthesia[preceding::incision = $i1]">
<xsl:copy-of select="current( )"/>
</xsl:if>
</xsl:template>
<!-- Wenn das Ergebnis nicht leer ist, kommt es in Kürze zur Anklage! Frage 5. Was passierte in Report1 zwischen dem ersten und dem zweiten Schnitt? -->
<xsl:template match="report">
<critical_sequence>
<!-- i1 = Erster Einschnitt im gesamten Bericht -->
<xsl:variable name="i1" select="(.//incision)[1]"/>
<!-- i2 = Zweiter Einschnitt im gesamten Bericht -->
<xsl:variable name="i2" select="(.//incision)[2]"/>
<!-- Kopiere alle auf i1 folgenden Geschwisterknoten, die kein Vorgängerelement i2 haben und nicht selbst i2 sind -->
<xsl:for-each select="$i1/following-sibling::node( ) [not(./preceding::incision = $i2) and not(. = $i2)]">
<xsl:copy-of select="."/>
</xsl:for-each>
</critical_sequence>
</xsl:template>Atención:
Las consultas 4 y 5 suponen cadenas únicas para elementosincision. Cierto aquí, no está garantizado en general. Para precisión use la receta Hallar el último día del mes. La prueba de la consulta 4 sería:
test=".//anesthesia[count(./preceding::incision | $i1) = count(./preceding::incision)]"Caso «R»: Datos relacionales.
Las consultas XML deben acceder a datos relacionales. Una base puede exponer cada tabla como XML, con su elemento raízDocumentrepresentando la tabla, hijos para filas y sus hijos para columnas. Columnas anulables son opcionales; ausencia significa null.Una base de subastas tiene USERS con ID únicos. Los usuarios venden o pujan. ITEMS enumera ofertas actuales y recientes con ID vendedores. BIDS guarda pujas por ID y número de objeto.
Solo se implementa una parte. Complete las demás para practicar XSLT; véanse los tres ejemplos siguientes.
Ejemplo: users.xml
<users>
<user_tuple>
<userid>U01</userid>
<name>Tom Jones</name>
<rating>B</rating>
</user_tuple>
<user_tuple>
<userid>U02</userid>
<name>Mary Doe</name>
<rating>A</rating>
</user_tuple>
<user_tuple>
<userid>U03</userid>
<name>Dee Linquent</name>
<rating>D</rating>
</user_tuple>
<user_tuple>
<userid>U04</userid>
<name>Roger Smith</name>
<rating>C</rating>
</user_tuple>
<user_tuple>
<userid>U05</userid>
<name>Jack Sprat</name>
<rating>B</rating>
</user_tuple>
<user_tuple>
<userid>U06</userid>
<name>Rip Van Winkle</name>
<rating>B</rating>
</user_tuple>
</users>Ejemplo: items.xml
<items>
<item_tuple>
<itemno>1001</itemno>
<description>Red Bicycle</description>
<offered_by>U01</offered_by>
<start_date>99-01-05</start_date>
<end_date>99-01-20</end_date>
<reserve_price>40</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1002</itemno>
<description>Motorcycle</description>
<offered_by>U02</offered_by>
<start_date>99-02-11</start_date>
<end_date>99-03-15</end_date>
<reserve_price>500</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1003</itemno>
<description>Old Bicycle</description>
<offered_by>U02</offered_by>
<start_date>99-01-10</start_date>
<end_date>99-02-20</end_date>
<reserve_price>25</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1004</itemno>
<description>Tricycle</description>
<offered_by>U01</offered_by>
<start_date>99-02-25</start_date>
<end_date>99-03-08</end_date>
<reserve_price>15</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1005</itemno>
<description>Tennis Racket</description>
<offered_by>U03</offered_by>
<start_date>99-03-19</start_date>
<end_date>99-04-30</end_date>
<reserve_price>20</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1006</itemno>
<description>Helicopter</description>
<offered_by>U03</offered_by>
<start_date>99-05-05</start_date>
<end_date>99-05-25</end_date>
<reserve_price>50000</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1007</itemno>
<description>Racing Bicycle</description>
<offered_by>U04</offered_by>
<start_date>99-01-20</start_date>
<end_date>99-02-20</end_date>
<reserve_price>200</reserve_price>
</item_tuple>
<item_tuple>
<itemno>1008</itemno>
<description>Broken Bicycle</description>
<offered_by>U01</offered_by>
<start_date>99-02-05</start_date>
<end_date>99-03-06</end_date>
<reserve_price>25</reserve_price>
</item_tuple>
</items>Ejemplo: bids.xml
<bids>
<bid_tuple>
<userid>U02</userid>
<itemno>1001</itemno>
<bid>35</bid>
<bid_date>99-01-07 </bid_date>
</bid_tuple>
<bid_tuple>
<userid>U04</userid>
<itemno>1001</itemno>
<bid>40</bid>
<bid_date>99-01-08</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U02</userid>
<itemno>1001 </itemno>
<bid>45</bid>
<bid_date>99-01-11</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U04</userid>
<itemno>1001</itemno>
<bid>50</bid>
<bid_date>99-01-13</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U02</userid>
<itemno>1001</itemno>
<bid>55</bid>
<bid_date>99-01-15</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U01</userid>
<itemno>1002</itemno>
<bid>400</bid>
<bid_date>99-02-14</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U02</userid>
<itemno>1002</itemno>
<bid>600</bid>
<bid_date>99-02-16</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U03</userid>
<itemno>1002</itemno>
<bid>800</bid>
<bid_date>99-02-17</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U04</userid>
<itemno>1002</itemno>
<bid>1000</bid>
<bid_date>99-02-25</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U02</userid>
<itemno>1002</itemno>
<bid>1200</bid>
<bid_date>99-03-02</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U04</userid>
<itemno>1003</itemno>
<bid>15</bid>
<bid_date>99-01-22</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U05</userid>
<itemno>1003</itemno>
<bid>20</bid>
<bid_date>99-02-03</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U01</userid>
<itemno>1004</itemno>
<bid>40</bid>
<bid_date>99-03-05</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U03</userid>
<itemno>1007</itemno>
<bid>175</bid>
<bid_date>99-01-25</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U05</userid>
<itemno>1007</itemno>
<bid>200</bid>
<bid_date>99-02-08</bid_date>
</bid_tuple>
<bid_tuple>
<userid>U04</userid>
<itemno>1007</itemno>
<bid>225</bid>
<bid_date>99-02-12</bid_date>
</bid_tuple>
</bids><!-- Frage 1. Liste die Objektnummer und die Beschreibung aller Fahrräder auf, die momentan in einer Auktion angeboten werden, sortiert nach Objektnummer: -->
<xsl:include href="../date/date.date-time.xslt"/>
<!-- Damit das Ergebnis wie das W3C-Beispiel aussieht -->
<xsl:param name="today" select="'1999-01-21'"/>
<xsl:template match="items">
<xsl:variable name="today-abs">
<xsl:call-template name="date:date-to-absolute-day">
<xsl:with-param name="date" select="$today"/>
</xsl:call-template>
</xsl:variable>
<result>
<xsl:for-each select="item_tuple">
<xsl:sort select="itemno" data-type="number"/>
<xsl:variable name="start-abs">
<xsl:call-template name="date:date-to-absolute-day">
<xsl:with-param name="date" select="start_date"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="end-abs">
<xsl:call-template name="date:date-to-absolute-day">
<xsl:with-param name="date" select="end_date"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$start-abs <= $today-abs and $end-abs >= $today-abs and contains(description, 'Bicycle')">
<xsl:copy>
<xsl:copy-of select="itemno"/>
<xsl:copy-of select="description"/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 2. Liste für alle Fahrräder die Objektnummer, die Beschreibung und das höchste Gebot (falls vorhanden) auf, sortiert nach Objektnummer: -->
<xsl:include href="../math/math.max.xslt"/>
<xsl:template match="items">
<result>
<xsl:for-each select="item_tuple[contains(description,'Bicycle')]">
<xsl:sort select="itemno" data-type="number"/>
<xsl:variable name="bids" select="document('bids.xml')//bid_tuple[itemno=current()/itemno]/bid"/>
<xsl:variable name="high-bid">
<xsl:call-template name="math:max">
<xsl:with-param name="nodes" select="$bids"/>
</xsl:call-template>
</xsl:variable>
<xsl:copy>
<xsl:copy-of select="itemno"/>
<xsl:copy-of select="description"/>
<high_bid><xsl:if test="$bids"><xsl:value-of select="$high-bid"/></xsl:if></high_bid>
</xsl:copy>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 3. Suche Fälle, in denen ein Benutzer mit einer Bewertung schlechter (alphabetisch: größer) als "C" ein Objekt mit einem Mindestpreis von mehr als 1.000 anbietet: -->
<!-- Nicht unbedingt notwendig, aber die Spezifikation definiert kein Bewertungssystem, weshalb wir es dynamisch ableiten! -->
<xsl:variable name="ratings">
<xsl:for-each select="document('users.xml')//user_tuple/rating">
<xsl:sort select="." data-type="text"/>
<xsl:if test="not(. = ./preceding::rating)">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="items">
<result>
<xsl:for-each select="item_tuple[reserve_price > 1000]">
<xsl:variable name="user" select="document('users.xml')//user_tuple[userid = current()/offered_by]"/>
<xsl:if test="string-length(substring-before($ratings,$user/rating)) > string-length(substring-before($ratings,'C'))">
<warning>
<xsl:copy-of select="$user/name"/>
<xsl:copy-of select="$user/rating"/>
<xsl:copy-of select="description"/>
<xsl:copy-of select="reserve_price"/>
</warning>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 4. Liste Objektnummern und Beschreibungen von Objekten auf, für die es keine Gebote gibt: -->
<xsl:template match="items">
<result>
<xsl:for-each select="item_tuple">
<xsl:if test="not(document('bids.xml')//bid_tuple[itemno = current()/itemno])">
<no_bid_item>
<xsl:copy-of select="itemno"/>
<xsl:copy-of select="description"/>
</no_bid_item>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>Caso «SGML»: Standard Generalized Markup Language.
Estas consultas y el documento se crearon para una conferencia de 1992 sobre Standard Generalized Markup Language (SGML). Para este uso, la Document Type Definition (DTD) y el ejemplo se convirtieron a XML.Se omiten por parecerse a las otras.
Caso «TEXT»: Búsqueda de texto completo.
Combina perfiles de empresas y noticias de relaciones públicas, fusiones y adquisiciones. Ilustra búsquedas textuales y resultados que combinan perfiles y noticias.Los nombres de empresas se buscan por palabras, sin distinguir mayúsculas y con espacios arbitrarios.
Todas son posibles en XSLT 1.0, pero pueden exigir muchas búsquedas: una cadena de un conjunto en otra o límites de frases.
Las técnicas de XPath sugieren soluciones. Muchas búsquedas exigen utilidades genéricas, tratadas en Ampliar e integrar XSLT, que retoma consultas complejas. Aquí resolvemos dos casos simples y enumeramos las demás resaltando dificultades.
<!-- Frage 1. Suche alle News-Objekte, in denen der Name "Foobar Corporation" im Titel auftaucht: -->
<xsl:template match="news">
<result>
<xsl:copy-of select="news_item/title[contains(., 'Foobar Corporation')]"/>
</result>
</xsl:template>
<!-- Frage 2. Erzeuge für jedes News-Objekt, das für die Gorilla Corporation relevant ist, ein "item summary"-Element (Objektzusammenfassung). Der Inhalt der Objektzusammenfassung besteht aus dem Titel, dem Datum und dem ersten Absatz des News-Objekts, getrennt durch Punkte. Ein News-Objekt ist relevant, wenn der Name des Unternehmens irgendwo im Inhalt des News-Objekts erwähnt wird: -->
<xsl:template match="news">
<result>
<xsl:for-each select="news_item[contains(content,'Gorilla Corporation')]">
<item_summary>
<xsl:value-of select="normalize-space(title)"/>. <xsl:text/>
<xsl:value-of select="normalize-space(date)"/>. <xsl:text/>
<xsl:value-of select="normalize-space(content/par[1])"/>
</item_summary>
</xsl:for-each>
</result>
</xsl:template>Caso «PARTS»: Descomposición recursiva.
Una consulta recursiva construye jerarquía profunda desde datos planos.La base describe piezas contenidas en otras.
La entrada plana representa cada pieza mediante elemento
<part>con atributospartidyname. Una pieza puede pertenecer a otra, cuyopartidfigura en el atributopartof. En una tabla relacional,partidsería clave primaria ypartofuna clave foránea que referenciapartid. La consulta transforma enlaces en jerarquía. La entrada sigue esta DTD:
<!partlist DOCTYPE [
<!ELEMENT partlist (part*)>
<!ELEMENT part EMPTY>
<!ATTLIST part
partid CDATA #REQUIRED
partof CDATA #IMPLIED
name CDATA #REQUIRED>
]>Los atributos
partidypartofpodrían ser ID e IDREF pero aquí son caracteres, quizá para facilitar extracción. Cada atributopartofselecciona unpartid. Piezas sin atributopartofno están contenidas en otra.La salida sigue esta DTD:
<!DOCTYPE parttree [
<!ELEMENT parttree (part*)>
<!ELEMENT part (part*)>
<!ATTLIST part
partid CDATA #REQUIRED
name CDATA #REQUIRED>
]>Ejemplo conforme:
<?xml version="1.0" encoding="ISO-8859-1"?>
<partlist>
<part partid="0" name="car"/>
<part partid="1" partof="0" name="engine"/>
<part partid="2" partof="0" name="door"/>
<part partid="3" partof="1" name="piston"/>
<part partid="4" partof="2" name="window"/>
<part partid="5" partof="2" name="lock"/>
<part partid="10" name="skateboard"/>
<part partid="11" partof="10" name="board"/>
<part partid="12" partof="10" name="wheel"/>
<part partid="20" name="canoe"/>
</partlist>
<!-- Frage 1. Konvertiere das Beispieldokument aus dem "partlist"- in das "parttree"-Format (siehe den DTD-Abschnitt für Definitionen). Im resultierenden Dokument wird die Teilezugehörigkeit dadurch repräsentiert, dass ein <part>-Element in einem anderen enthalten ist. Jedes Teil, das nicht Bestandteil eines anderen Teils ist, sollte als separates Top-Level-Dokument im Ausgabedokument auftauchen: -->
<xsl:template match="partlist">
<parttree>
<!-- Beginne mit dem Teil, das nicht Bestandteil eines anderen ist -->
<xsl:apply-templates select="part[not(@partof)]"/>
</parttree>
</xsl:template>
<xsl:template match="part">
<part partid="{@partid}" name="{@name}">
<xsl:apply-templates select="../part[@partof = current( )/@partid]"/>
</part>
</xsl:template>
<!-- Es stellt sich heraus, dass diese Art der Transformation in XSLT einfacher zu kodieren und zu verstehen ist als in XQuery. Zum Vergleich kommt hier die XQuery-Lösung, die vom W3C-Artikel angeboten wird: -->
define function one_level (element $p) returns element
{
<part partid="{ $p/@partid }" name="{ $p/@name }" >
{
for $s in document("partlist.xml")//part
where $s/@partof = $p/@partid
return one_level($s)
}
</part>
}
<parttree>
{
for $p in document("partlist.xml")//part[empty(@partof)]
return one_level($p)
}
</parttree>Incluso sin conocer XQuery se ve su recursión explícita, mientras apply-templates y patrones XSLT permiten una solución más declarativa. La diferencia es pequeña, pero encuentro XSLT más elegante.
Caso «REF»: Consultas de referencias, retiradas de la última versión W3C.
Las referencias importan en XML. Este caso presenta una base rica en referencias y consultas.El archivo census.xml contiene un elemento por persona censada. Cada elemento
personguarda nombre, ocupación y cónyuge opcional como atributos. El atributospousees IDREF hacia el atributo IDnamedel elementospouse.La filiación usa anidación bajo un padre, nunca ambos, considerando muertes, divorcios y nuevos matrimonios. Los hijos de X incluyen los del cónyuge: si Joe contiene Sam y Martha Dave, ambos son hijos de ambos cónyuges. Cada persona tiene cero, uno o dos padres.
El documento de entrada census.xml sigue esta DTD:
<!DOCTYPE census [
<!ELEMENT census (person*)>
<!ELEMENT person (person*)>
<!ATTLIST person
name ID #REQUIRED
spouse IDREF #IMPLIED
job CDATA #IMPLIED >
]>Ejemplo: Dos familias amigas ligadas por varios matrimonios:
<census>
<person name="Bill" job="Teacher">
<person name="Joe" job="Painter" spouse="Martha">
<person name="Sam" job="Nurse">
<person name="Fred" job="Senator" spouse="Jane"></person>
</person>
<person name="Karen" job="Doctor" spouse="Steve"></person>
</person>
<person name="Mary" job="Pilot">
<person name="Susan" job="Pilot" spouse="Dave"></person>
</person>
</person>
<person name="Frank" job="Writer">
<person name="Martha" job="Programmer" spouse="Joe">
<person name="Dave" job="Athlete" spouse="Susan"></person>
<person name="John" job="Artist">
<person name="Helen" job="Athlete"></person>
<person name="Steve" job="Accountant" spouse="Karen">
<person name="Jane" job="Doctor" spouse="Fred"></person>
</person>
</person>
</person>
</person>
</census>
<!-- Frage 1. Suche Marthas Ehepartner: -->
<xsl:strip-space elements="*"/>
<xsl:template match="person[@spouse='Martha']">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
</xsl:template>
<!-- Frage 2. Suche Eltern von Sportlern: -->
<xsl:template match="census">
<xsl:variable name="everyone" select="//person"/>
<result>
<!-- Für jede Person mit Kindern -->
<xsl:for-each select="$everyone[person]">
<xsl:variable name="spouse" select="$everyone[@spouse=current( )/@name]"/>
<xsl:if test="./person/@job = 'Athlete' or $spouse/person/@job = 'Athlete'">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 3. Suche Leute, die den gleichen Beruf haben wie einer ihrer Elternteile: -->Inténtelo.
<!-- Frage 4. Liste Namen von Eltern und Kindern auf, die den gleichen Beruf haben, und liste deren Berufe auf: -->
<xsl:template match="census">
<xsl:variable name="everyone" select="//person"/>
<result>
<!-- Für jede Person mit Kindern -->
<xsl:for-each select="$everyone[person]">
<xsl:variable name="spouse" select="$everyone[@spouse=current( )/@name]"/>
<xsl:apply-templates select="person[@job = current( )/@job]">
<xsl:with-param name="parent" select="@name"/>
</xsl:apply-templates>
<xsl:apply-templates select="person[@job = $spouse/@job]">
<xsl:with-param name="parent" select="$spouse/@name"/>
</xsl:apply-templates>
</xsl:for-each>
</result>
</xsl:template>
<xsl:template match="person">
<xsl:param name="parent"/>
<match parent="{$parent}" child="{@name}" job="{@job}"/>
</xsl:template>
<!-- Frage 5. Liste Namenspaare von Großeltern und Enkeln auf: -->
<xsl:template match="census">
<xsl:variable name="everyone" select="//person"/>
<result>
<!-- Für jedes Enkelkind -->
<xsl:for-each select="$everyone[../../../person]">
<!-- Hole Großelternteil1 (das garantiert existiert für jedes Enkelkind) -->
<grandparent name="{../../@name}" grandchild="{@name}"/>
<!-- Hole Großelternteil2, das der Ehepartner von Großelternteil1 ist, falls aufgeführt -->
<xsl:if test="../../@spouse">
<grandparent name="{../../@spouse}" grandchild="{@name}"/>
</xsl:if>
<!-- Hole die Namen des Ehepartners des Elternteils dieser Person (d.h. je nachdem die Mutter oder den Vater) -->
<xsl:variable name="spouse-of-parent" select="../@spouse"/>
<!-- Hole die Eltern des Ehepartners des Elternteils (spouse-of-parent), falls vorhanden -->
<xsl:variable name="gp3" select="$everyone[person/@name=$spouse-of-parent]"/>
<xsl:if test="$gp3">
<grandparent name="{$gp3/@name}" grandchild="{@name}"/>
<xsl:if test="$gp3/@spouse">
<grandparent name="{$gp3/@spouse}" grandchild="{@name}"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 6. Suche Leute ohne Kinder: -->
<xsl:strip-space elements="*"/>
<xsl:template match="census">
<xsl:variable name="everyone" select="//person"/>
<result>
<xsl:for-each select="$everyone[not(./person)]">
<xsl:variable name="spouse" select="$everyone[@name = current( )/@spouse]"/>
<xsl:if test="not ($spouse) or not($spouse/person)">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
<!-- Frage 7. Liste die Namen aller Nachfahren von Joe auf. Zeige jeden Nachfahren als Element mit dem Namen des Nachfahrens als Inhalt und seinem oder ihrem Familienstatus sowie der Anzahl der Kinder als Attribute. Sortiere die Nachfahren erstens in absteigender Reihenfolge anhand der Anzahl der Kinder sowie zweitens alphabetisch nach dem Namen: -->
<xsl:variable name="everyone" select="//person"/>
<xsl:template match="census">
<result>
<xsl:apply-templates select="//person[@name='Joe']"/>
</result>
</xsl:template>
<xsl:template match="person">
<xsl:variable name="all-desc">
<xsl:call-template name="descendants">
<xsl:with-param name="nodes" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($all-desc)/*">
<xsl:sort select="count(./* | $everyone[@name = current( )/@spouse]/*)" order="descending" data-type="number"/>
<xsl:sort select="@name"/>
<xsl:variable name="mstatus" select="normalize-space(substring('No Yes',boolean(@spouse)* 3+1,3))"/>
<person married="{$mstatus}" nkids="{count(./* | $everyone[@name = current( )/@spouse]/*)}">
<xsl:value-of select="@name"/>
</person>
</xsl:for-each>
</xsl:template>
<xsl:template name="descendants">
<xsl:param name="nodes"/>
<xsl:param name="descendants" select="/.."/>
<xsl:choose>
<xsl:when test="not($nodes)">
<xsl:copy-of select="$descendants"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="descendants">
<xsl:with-param name="nodes" select="$nodes[position( ) > 1] | $nodes[1]/person | id($nodes[1]/@spouse)/person"/>
<xsl:with-param name="descendants" select="$descendants | $nodes[1]/person | id($nodes[1]/@spouse)/person"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>Esta consulta funciona, ¡pero es fea! Reunir descendientes ordenables exige la extensión
node-set. La funciónid( )no encuentra cónyuges porque depende del documento del nodo, aquí una copia. Deben buscarse elementosspousemediante una variable con todos los elementosperson. Compare XQuery:
define function descrip (element $e) returns element
{
let $kids := $e/* union $e/@spouse = > person/*
let $mstatus := if ($e[@spouse]) then "Yes" else "No"
return
<person married={ $mstatus } nkids={ count($kids) } > { $e/@name/text( ) }</person>
define function descendants (element $e)
{
if (empty($e/* union $e/@spouse = > person/*))
then $e
else $e union descendants($e/* union $e/@spouse = > person/*)
}
descrip(descendants(//person[@name = "Joe"])) sortby(@nkids descending, .)Discusión
XSLT 1.0
Este capítulo ofrece consultas preparadas en vez de genéricas. Varían mucho. La clasificación W3C y estas soluciones XSLT dan una base para muchas tareas.
Falta espacio para todo XQuery, pero comparar enfoques enseña; consulte el documento W3C.
Comentar cada consulta sería poco práctico. Bastan bases de XSLT. Alternativas pueden mejorar estas soluciones influidas por XQuery. Varié construcciones, a veces iterativas con xsl:for-each, a veces declarativas con patrones y xsl:apply-templates.
XSLT 2.0
Este capítulo largo no repite todo en 2.0, pero algunos problemas muestran mejoras con XPath 2.0 y XSLT 2.0:
Use
for-each-groupodistinct-values( )para eliminar duplicados.
<!-- Frage 4 (aus Beispiel "prices.xml"). Liste für jeden Autor in bib.xml den Namen des Autors und die Titel aller Bücher dieses Autors auf, und zwar in einem "result"-Element gruppiert: -->
<xsl:for-each-group select="//author" group-by=".">
<result>
<xsl:copy-of select="."/>
<xsl:for-each select="/bib/book[author eq current-grouping-key( )]">
<xsl:copy-of select="title"/>
</xsl:for-each>
</result>
</xsl:for-each-group>
<!-- Verwendung von distinct-values( ) -->
<xsl:for-each select="distinct-values(//author)">
<result>
<xsl:copy-of select="."/>
<xsl:for-each select="/bib/book[author eq .]">
<xsl:copy-of select="title"/>
</xsl:for-each>
</result>
</xsl:for-each>Para evitar copiar nodos, use
xsl:sequence.
<!-- Frage 8. Suchen Sie im Dokument books.xml alle Abschnitts-(section-) oder Kapiteltitel (chapter), die das Wort "XML" enthalten, und zwar ungeachtet ihrer Schachtelungsebene: -->
<results>
<xsl:sequence select="(//chapter | //section)/title)[contains(.,'XML')]"/>
</results>Use funciones y secuencias para simplificar consultas.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ckbk="http://www.ora.com/XSLTCkbk">
<xsl:key name="person-key" match="person" use="@name"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="everyone" select="//person" as="item( )*"/>
<xsl:template match="census">
<result>
<xsl:apply-templates select="//person[@name='Joe']"/>
</result>
</xsl:template>
<xsl:template match="person">
<!-- Kein Bedarf für die Konvertierung von Knotenmengen. Verwenden Sie die descendants-Funktion direkt -->
<xsl:for-each select="ckbk:descendants(.,/)[current( ) != .]">
<xsl:sort select="count(./* | $everyone[@name = current( )/@spouse]/*)" order="descending" data-type="number"/>
<xsl:sort select="@name"/>
<xsl:variable name="mstatus" select="if (@spouse) then 'Yes' else 'No'"/>
<person married="{$mstatus}" nkids="{count(./* | key('person-key', @spouse)/*)}">
<xsl:value-of select="@name"/>
</person>
</xsl:for-each>
</xsl:template>
<!-- Beachten Sie, dass diese Funktion einfacher ist als das Template in der XSLT 1.0-Lösung. Wir übergeben sie im Dokument, da sie in Funktionen unbekannt ist.-->
<xsl:function name="ckbk:descendants">
<xsl:param name="nodes" as="item( )*"/>
<xsl:param name="doc"/>
<xsl:sequence select="$nodes, for $person in $nodes return ckbk:descendants( ($person/person, key('person-key', $person/@spouse,$doc)/person), $doc)"/>
</xsl:function>
</xsl:stylesheet>Véase también
Evan Lenz también trata XSLT en lugar de XQuery en XQuery: Reinventing the Wheel?. Para una introducción detallada a XQuery, véase XQuery – Introducción a consultas XML.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
Puede imprimir la versión en línea para uso privado.
Para cualquier otro uso, este capítulo de «XSLT Kochbuch» está sujeto a las mismas condiciones que la edición encuadernada. La obra y todas sus partes están protegidas por derechos de autor. Todos los derechos reservados, incluidos reproducción, traducción, microfilmación, almacenamiento y tratamiento en sistemas electrónicos.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de