XSLT - GitHub Pages · XML + XSLT

Post on 04-Jul-2020

41 views 0 download

Transcript of XSLT - GitHub Pages · XML + XSLT

XSLT

XSLT is . . .

XML

XSLT

XSL-FO

XPath

Назначение

XML XML

XSLT

Назначение

XML HTML

XSLT

Сервер Браузер

Назначение

XML HTML

XSLT

Сервер Браузер

Declaration

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/XSL/Transform">...</xsl:stylesheet>

Declaration

<?xmlversion="1.0"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/XSL/Transform">...</xsl:stylesheet>

Рабочий пример<?xmlversion="1.0"?><page><sections><itemhref="politics">Политика</item><itemhref="world">Вмире</item><itemhref="economy">Экономика</item><itemhref="society">Общество</item></sections></page>

Рабочий пример<?xmlversion="1.0"?><page><sections><itemhref="politics"><name>Политика</name><itemhref="russia">ВРоссии</item><itemhref="foreign">Внешняя...</item><itemhref="senate">Совет...</item></item><itemhref="world">Вмире</item><itemhref="economy">Экономика</item><itemhref="society">Общество</item></sections></page>

Рабочий пример<?xmlversion="1.0"?><page><sections><itemhref="politics"><name>Политика</name><itemhref="russia"><name>ВРоссии</name></item><itemhref="foreign"><name>Внешняяполитика</name></item><itemhref="senate"><name>СоветФедерации</name></item></item>...</sections></page>

Рабочий пример. Вариант 1<?xmlversion="1.0"?><page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

Ожидаемый результат<ul><li><ahref="/politics/">Политика</a><ul><li><ahref="/politics/russia/">ВРоссии</a></li><li><ahref="/politics/foreign/">Внешняя...</a></li><li><ahref="/politics/senate/">ВРоссии</a></li></ul></li><li><ahref="/world/">Вмире</a></li><li><ahref="/society/">Общество</a></li></ul>

XSLT<?xmlversion="1.0"encoding="UTF‐8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:outputencoding="UTF‐8"indent="yes"/>

</xsl:stylesheet>

XSLT<?xmlversion="1.0"encoding="UTF‐8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:outputencoding="UTF‐8"indent="yes"/>

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

</xsl:stylesheet>

XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

XSLT и XPath

XPath

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a></xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

HTML

<?xmlversion="1.0"encoding="UTF‐8"?><div><ahref="politics">Политика</a><ahref="world">Вмире</a><ahref="economy">Экономика</a><ahref="society">Общество</a></div>

XML + XSLT

<xsl:templatematch="/page"><div><xsl:for‐eachselect="sections/item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a>?<xsl:for‐eachselect="item">?</xsl:for‐each></div></xsl:template>

<page><sections><itemhref="politics"name="Политика"><itemhref="russia"name="ВРоссии"/><itemhref="foreign"name="Внешняя..."/><itemhref="senate"name="Совет..."/></item><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/></sections></page>

XSLT

<xsl:templatematch="/page"><div><xsl:apply‐templatesselect="sections/item"/></div></xsl:template>

<xsl:templatematch="item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a><xsl:iftest="item"><div><xsl:apply‐templatesselect="item"/></div></xsl:if></xsl:template>

<xsl:templatematch="/page"><div><xsl:apply‐templatesselect="sections/item"/></div></xsl:template>

<xsl:templatematch="item"><ahref="{@href}"><xsl:value‐ofselect="@name"/></a><xsl:iftest="item"><div><xsl:apply‐templatesselect="item"/></div></xsl:if></xsl:template>

XML + XSLT

XML + XSLT = HTML

<?xmlversion="1.0"encoding="UTF‐8"?><div><ahref="politics">Политика</a><div><ahref="russia">ВРоссии</a><ahref="foreign">Внешняяполитика</a><ahref="senate">СоветФедерации</a></div><ahref="world">Вмире</a><ahref="economy">Экономика</a><ahref="society">Общество</a></div>

XML + XSLT = HTML

<?xmlversion="1.0"encoding="UTF‐8"?><div><ahref="/politics/">Политика</a><div><ahref="/politics/russia/">ВРоссии</a><ahref="/politics/foreign/">Внешняяполитика</a><ahref="/politics/senate/">СоветФедерации</a></div><ahref="/world/">Вмире</a><ahref="/economy/">Экономика</a><ahref="/society/">Общество</a></div>

XSLT

<xsl:templatematch="item"><ahref="/{@href}/"><xsl:value‐ofselect="@name"/></a>...</xsl:template>

XSLT

<xsl:templatematch="item"><ahref="/{@href}/"><xsl:iftest="parent::item"><xsl:attributename="href"><xsl:text>/</xsl:text><xsl:value‐ofselect="../item/@href"/><xsl:text>/</xsl:text><xsl:value‐ofselect="@href"/><xsl:text>/</xsl:text></xsl:attribute></xsl:if><xsl:value‐ofselect="@name"/></a>...</xsl:template>

Рабочий пример. Вариант 2<?xmlversion="1.0"?><page><sections><itemhref="politics"name="Политика"/><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/><itemhref="russia"name="ВРоссии"parent="politics"/><itemhref="foreign"name="Внешняя..."parent="politics"/><itemhref="senate"name="Совет..."parent="politics"/></sections></page>

Рабочий пример. Вариант 2<?xmlversion="1.0"?><page><sections><itemhref="politics"name="Политика"/><itemhref="world"name="Вмире"/><itemhref="economy"name="Экономика"/><itemhref="society"name="Общество"/><itemhref="russia"name="ВРоссии"parent="politics"/><itemhref="foreign"name="Внешняя..."parent="politics"/><itemhref="senate"name="Совет..."parent="politics"/></sections></page>

XSLT<xsl:templatematch="/page"><div><xsl:apply‐templatesselect="sections/item[not(@parent)]"/></div></xsl:template>

XSLT<xsl:templatematch="item"><ahref="/{@href}/"><xsl:iftest="@parent"><xsl:attributename="href"><xsl:text>/</xsl:text><xsl:value‐ofselect="@parent"/><xsl:text>/</xsl:text><xsl:value‐ofselect="@href"/><xsl:text>/</xsl:text></xsl:attribute></xsl:if><xsl:value‐ofselect="@name"/></a><xsl:iftest="../item[@parent=current()/@href]"><div><xsl:apply‐templatesselect="../item[@parent=current()/@href]"/></div></xsl:if></xsl:template>

XSLT<xsl:templatematch="item"><ahref="/{@parent}/{@href}/"><xsl:value‐ofselect="@name"/></a><xsl:iftest="../item[@parent=current()/@href]"><div><xsl:apply‐templatesselect="../item[@parent=current()/@href]"/></div></xsl:if></xsl:template>

Элементы XSLT<xsl:importhref="another.xslt"/>

<xsl:includehref="more.xslt"/>

<xsl:output

method="html"|"xml"|"text"

encoding="KOI8‐R"

indent="yes"|"no"

/>

Элементы XSLT<xsl:importhref="another.xslt"/>

<xsl:includehref="more.xslt"/>

<xsl:output

method="html"|"xml"|"text"

encoding="KOI8‐R"

indent="yes"|"no"

/>

<xsl:templatematch="para">

</xsl:template>

<xsl:templatename="template‐name">

</xsl:template>

Последовательность обхода XSLT<xsl:templatematch="/">

</xsl:template>

<xsl:templatematch="/page">

</xsl:template>

<xsl:templatematch="/page/sections">

</xsl:template>

<xsl:templatename="sections/item">

</xsl:template>

<xsl:templatename="item">

</xsl:template>

<xsl:templatematch="item[item]"/>

Обработка узлов (создание дерева)<xsl:templatematch="/">

<html>

<head>

<xsl:apply‐templatesselect="css"/>

<xsl:apply‐templatesselect="js"/>

</head>

<body>

<xsl:apply‐templatesselect="content"/>

</body>

</html>

</xsl:template>

Обработка узлов<xsl:templatematch="/">

<html>

<head>

<xsl:apply‐templatesselect="css"/>

<xsl:apply‐templatesselect="js"/>

</head>

<body>

<xsl:apply‐templatesselect="content"/>

</body>

</html>

</xsl:template>

<xsl:templatematch="page">

<xsl:apply‐templatesselect="."/>

</xsl:template>

Обработка узлов<css>

<item>default.css</item>

<item>weather.css</item>

</css>

<xsl:apply‐templatesselect="css"/>

<xsl:templatematch="css">

<xsl:for‐eachselect="item">

<link

rel="stylesheet"

type="text/css"

href="text()"

/>

</xsl:for‐each>

</xsl:template>

Обработка узлов<js>

<item>default.js</item>

<item>gwt.js</item>

</js>

<xsl:apply‐templatesselect="js"/>

<xsl:templatematch="js">

<xsl:apply‐templatesselect="item"/>

</xsl:template>

<xsl:templatematch="js/item">

<scripttype="text/javascript"src="text()">

</script>

</xsl:template>

mode<xsl:templatematch="css"mode="header">

<xsl:for‐eachselect="item">

<linkrel="stylesheet"type="text/css"

href="{text()}"/>

</xsl:for‐each>

</xsl:template>

<xsl:templatematch="css"mode="debug">

<ul>

<xsl:for‐eachselect="item">

<li>

<xsl:value‐ofselect="text()"/>

</li>

</xsl:for‐each>

</ul>

</xsl:template>

mode<xsl:templatematch="/">

<html>

<head>

<xsl:apply‐templates

select="css"

mode="header"

/>

</head>

<body>

<xsl:apply‐templates

select="css"

mode="debug"

/>

</body>

</html>

</xsl:template>

Шаблон с именем<xsl:templatename="copywright">

<divid="Copywright">

<xsl:text>©</xsl:text>

<xsl:value‐ofselect="/page/manifest/date/@year”/>

</div>

</xsl:template>

<xsl:templatematch="/">

...

<body>

<xsl:apply‐templatesselect="content"/>

<xsl:call‐templatename="copywright"/>

</body>

...

</xsl:template>

Создание элементов<xsl:elementname="body">

<xsl:copy‐of

select="/page/content/*|/page/content/text()"/>

</xsl:element>

Создание элементов и аттрибутов<xsl:elementname="body">

<xsl:attributename="style">

<xsl:text>background:white</xsl:text>

</xsl:attribute>

<xsl:copy‐of

select="/page/content/*|/page/content/text()"/>

</xsl:element>

Создание элементов и аттрибутов<xsl:elementname="body">

<xsl:attributename="style">

<xsl:text>background:white</xsl:text>

</xsl:attribute>

<xsl:copy‐of

select="/page/content/*|/page/content/text()"/>

</xsl:element>

≈<bodystyle="background:white">

<xsl:apply‐templatesselect="/page/content"/>

</body>

Комментарии<!‐‐xmlcomment‐‐>

<xsl:textdisable‐output‐escaping="yes">

&lt;!‐‐strangecomment‐‐&gt;

</xsl:text>

<xsl:comment>

xsltcomment

</xsl:comment>

Нумерация<xsl:number

level="single"|"multiple"|"any"

from="sections"

count="item"

format="A"|"=1:2="

/>

Нумерация<xsl:templatematch="item">

<xsl:numberformat="multiple"/>

<ahref="{@href}">

<xsl:value‐ofselect="{text()}"/>

</a>

...

</xsl:template>

<div>

1<ahref="/politics/">Политика</a>

<div>

1.1<ahref="/russia/">ВРоссии</a>

1.2<ahref="/foreign/">Внешняяполитика</a>

1.3<ahref="/senate/">СоветФедерации</a>

</div>

...

Условия<xsl:iftest="@colour=‘white’">

<xsl:apply‐templates/>

</xsl:if>

Условия<xsl:iftest="@colour=‘white’">

<xsl:apply‐templates/>

</xsl:if>

<xsl:choose>

<xsl:whentest="@size<10">

...

</xsl:when>

<xsl:whentest="@size=10">

...

</xsl:when>

<xsl:otherwise>

...

</xsl:otherwise>

</xsl:choose>

Условия<xsl:iftest="@colour=‘white’">

<xsl:apply‐templates/>

</xsl:if>

<xsl:choose>

<xsl:whentest="@size<10">

...

</xsl:when>

<xsl:whentest="@size=10">

...

</xsl:when>

<xsl:otherwise>

...

</xsl:otherwise>

</xsl:choose>

Сортировка<xsl:sort

select="@href"

order="ascending"|"descending"

case‐order="upper‐first"|"lower‐first"

/>

<xsl:for‐eachselect="item">

<xsl:sortselect="text()"/>

<ahref="{@href}">

<xsl:value‐ofselect="text()"/>

</a>

</xsl:for‐each>

Переменные<xsl:variablename="year"select="2007"/>

<xsl:value‐ofselect="$year"/>

Переменные<xsl:variablename="year"select="2007"/>

<xsl:value‐ofselect="$year"/>

<xsl:variablename="copywright">

<p>

<xsl:value‐ofselect="$year"/>

</p>

<ul>

<xsl:for‐eachselect="authors/item">

<li>

<xsl:value‐ofselect="@name"/>

</li>

</xsl:for‐each>

</ul>

</xsl:variable>

Переменные<xsl:templatematch="item">

<xsl:paramname="class"/>

<divclass="$class">

...

</div>

</xsl:template>

<xsl:apply‐templatesselect="item">

<xsl:with‐paramname="class"select="’front’"/>

</xsl:apply‐templates>

Переменные<xsl:templatematch="item">

<xsl:paramname="class"/>

<divclass="$class">

...

</div>

</xsl:template>

<xsl:apply‐templatesselect="item">

<xsl:with‐paramname="class">front</xsl:with‐param>

</xsl:apply‐templates>

Переменные<xsl:variable

name="list"

select="document(‘site‐structure.xml’)"

/>

<xsl:templatename="site‐menu">

<xsl:for‐eachselect="$list/sections/item">

<ahref="{@href}">

<xsl:value‐ofselect="text()"/>

</a>

</xsl:for‐each>

</xsl:template>

Переменные<xsl:variable

name="list"

select="document(‘http://example.com/structure.xml’)"

/>

<xsl:templatename="site‐menu">

<xsl:for‐eachselect="$list/sections/item">

<ahref="{@href}">

<xsl:value‐ofselect="text()"/>

</a>

</xsl:for‐each>

</xsl:template>

Ключи<xsl:key

name="children"

match="/page/sections/item"

use="@parent"/>

<xsl:templatematch="/page/sections">

<xsl:for‐eachselect="item[not(@parent)]">

<ahref="{@href}">

<xsl:value‐ofselect="@name"/>

</a>

<xsl:for‐eachselect="key('children',@href)">

<xsl:value‐ofselect="@name"/>

<xsl:iftest="position()!=last()">,</xsl:if>

</xsl:for‐each>

</xsl:for‐each>

</xsl:template>