XSLT - GitHub Pages · XML + XSLT

61
XSLT

Transcript of XSLT - GitHub Pages · XML + XSLT

Page 1: XSLT - GitHub Pages · XML + XSLT

XSLT

Page 2: XSLT - GitHub Pages · XML + XSLT

XSLT is . . .

XML

XSLT

XSL-FO

XPath

Page 3: XSLT - GitHub Pages · XML + XSLT

Назначение

XML XML

XSLT

Page 4: XSLT - GitHub Pages · XML + XSLT

Назначение

XML HTML

XSLT

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

Page 5: XSLT - GitHub Pages · XML + XSLT

Назначение

XML HTML

XSLT

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

Page 6: XSLT - GitHub Pages · XML + XSLT

Declaration

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

Page 7: XSLT - GitHub Pages · XML + XSLT

Declaration

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

Page 8: XSLT - GitHub Pages · XML + XSLT

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

Page 9: XSLT - GitHub Pages · XML + XSLT

Рабочий пример<?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>

Page 10: XSLT - GitHub Pages · XML + XSLT

Рабочий пример<?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>

Page 11: XSLT - GitHub Pages · XML + XSLT

Рабочий пример. Вариант 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>

Page 12: XSLT - GitHub Pages · XML + XSLT

Ожидаемый результат<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>

Page 13: XSLT - GitHub Pages · XML + XSLT

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>

Page 14: XSLT - GitHub Pages · XML + XSLT

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>

Page 15: XSLT - GitHub Pages · XML + XSLT

XSLT

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

Page 16: XSLT - GitHub Pages · XML + XSLT

<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

Page 17: XSLT - GitHub Pages · XML + XSLT

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>

Page 18: XSLT - GitHub Pages · XML + XSLT

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>

Page 19: XSLT - GitHub Pages · XML + XSLT

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>

Page 20: XSLT - GitHub Pages · XML + XSLT

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>

Page 21: XSLT - GitHub Pages · XML + XSLT

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>

Page 22: XSLT - GitHub Pages · XML + XSLT

HTML

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

Page 23: XSLT - GitHub Pages · XML + XSLT

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>

Page 24: XSLT - GitHub Pages · XML + XSLT

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>

Page 25: XSLT - GitHub Pages · XML + 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>

XML + XSLT

Page 26: XSLT - GitHub Pages · 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>

Page 27: XSLT - GitHub Pages · XML + XSLT

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>

Page 28: XSLT - GitHub Pages · XML + XSLT

XSLT

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

Page 29: XSLT - GitHub Pages · XML + XSLT

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>

Page 30: XSLT - GitHub Pages · XML + XSLT

Рабочий пример. Вариант 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>

Page 31: XSLT - GitHub Pages · XML + XSLT

Рабочий пример. Вариант 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>

Page 32: XSLT - GitHub Pages · XML + XSLT

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

Page 33: XSLT - GitHub Pages · XML + XSLT

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>

Page 34: XSLT - GitHub Pages · XML + XSLT

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>

Page 35: XSLT - GitHub Pages · XML + XSLT

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

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

<xsl:output

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

encoding="KOI8‐R"

indent="yes"|"no"

/>

Page 36: XSLT - GitHub Pages · XML + XSLT

Элементы 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>

Page 37: XSLT - GitHub Pages · XML + XSLT

Последовательность обхода 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]"/>

Page 38: XSLT - GitHub Pages · XML + XSLT

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

<html>

<head>

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

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

</head>

<body>

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

</body>

</html>

</xsl:template>

Page 39: XSLT - GitHub Pages · XML + XSLT

Обработка узлов<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>

Page 40: XSLT - GitHub Pages · XML + XSLT

Обработка узлов<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>

Page 41: XSLT - GitHub Pages · XML + XSLT

Обработка узлов<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>

Page 42: XSLT - GitHub Pages · XML + XSLT

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>

Page 43: XSLT - GitHub Pages · XML + XSLT

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>

Page 44: XSLT - GitHub Pages · XML + XSLT

Шаблон с именем<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>

Page 45: XSLT - GitHub Pages · XML + XSLT

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

<xsl:copy‐of

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

</xsl:element>

Page 46: XSLT - GitHub Pages · XML + XSLT

Создание элементов и аттрибутов<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>

Page 47: XSLT - GitHub Pages · XML + XSLT

Создание элементов и аттрибутов<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>

Page 48: XSLT - GitHub Pages · XML + XSLT

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

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

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

</xsl:text>

<xsl:comment>

xsltcomment

</xsl:comment>

Page 49: XSLT - GitHub Pages · XML + XSLT

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

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

from="sections"

count="item"

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

/>

Page 50: XSLT - GitHub Pages · XML + XSLT

Нумерация<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>

...

Page 51: XSLT - GitHub Pages · XML + XSLT

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

<xsl:apply‐templates/>

</xsl:if>

Page 52: XSLT - GitHub Pages · XML + XSLT

Условия<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>

Page 53: XSLT - GitHub Pages · XML + XSLT

Условия<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>

Page 54: XSLT - GitHub Pages · XML + XSLT

Сортировка<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>

Page 55: XSLT - GitHub Pages · XML + XSLT

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

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

Page 56: XSLT - GitHub Pages · XML + XSLT

Переменные<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>

Page 57: XSLT - GitHub Pages · XML + XSLT

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

<xsl:paramname="class"/>

<divclass="$class">

...

</div>

</xsl:template>

<xsl:apply‐templatesselect="item">

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

</xsl:apply‐templates>

Page 58: XSLT - GitHub Pages · XML + XSLT

Переменные<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>

Page 59: XSLT - GitHub Pages · XML + XSLT

Переменные<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>

Page 60: XSLT - GitHub Pages · XML + XSLT

Переменные<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>

Page 61: XSLT - GitHub Pages · XML + XSLT

Ключи<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>