📜 ⬆️ ⬇️

Practical XSLT. Use as a template engine

There are plenty of XSL documentation available online. This section does not pretend to the role of documentation on the language, but only briefly, step by step explains how to create your XSLT template.

The scheme described below has been used successfully by me for more than 3 years. At first, I was very cautious about XSLT (especially when sorting out other people's sources), but once I realized what it was, I no longer know how to work without it.

Desktop

Let's define what we need for work:

I have an input XML document issued by a CMS-system, in which each page with the material is collected in an XML-tree .
')
There are no limitations to the XHTML layout . There are only certain recommendations for the layout, which will significantly save time on the formation of the template.

As a parser (collector) of the final document, you can use the browser. You just need to specify the path to the template file in XML documents :
<?xml-stylesheet type="text/xsl" href="template.xsl" ?>

Although, as practice has shown, this mechanism is quite buggy (I had to use IE ). It is better to use the tools of XML-parsing of the language in which the CMS-system is written. I use Parser (on it, in general, the whole system works for me).

Input XML Document

First, let's deal with the input XML document . In order to use XSL you need to have a complete understanding of its structure.

I use the following scheme:
<?xml version="1.0" encoding="windows-1251"?>



<lang_table>
/>
</lang_table>
<item id="0" parent_id="0" is_published="1" section="1">

/

<item id="1" parent_id="0" is_published="1" section="1">

news







The above example of the scheme does not claim to be optimal. For one reason or another, it is convenient for me. But first things first.

<?xml version="1.0" encoding="windows-1251"?> - the header of the XML file . Must go strictly from the beginning of the file. It contains the version of the XML language used and the encoding of the document. I usually work in windows-1251 (for now it's more convenient), but, in theory, UTF-8 is better.

- ( ). :
Lang - . . Id - .
<lang_table> - , .
- :
- ( ):
<item id="0" parent_id="0" is_published="1" section="1">
- . :
Id - . Parent_id - . Is_published - . Dir - uri- . . Section - . .
- .
CMS : :
Html - . , . Com - -. , CMS: , , ..
XSL- , . .

CMS , :
:
Id - .
Container - - ( ).
Sorting - .
Type - :
Com - -
Html - .
Method - .
Title - .
DTD ( ):
<!DOCTYPE site_page [

<!ENTITY nbsp " ">
<!ENTITY sect "§" >
<!ENTITY copy "©">
<!ENTITY laquo "«">
<!ENTITY reg "®">
<!ENTITY deg "°">
<!ENTITY plusmn "±">
<!ENTITY para "¶">
<!ENTITY raquo "»">
<!ENTITY times "×">



<!ENTITY bull "•">
<!ENTITY hellip "…">



<!ENTITY ndash "–">
<!ENTITY mdash "—">
<!ENTITY lsquo "'">
<!ENTITY rsquo "'">
<!ENTITY sbquo "‚">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY bdquo "„">
<!ENTITY lsaquo "‹">
<!ENTITY rsaquo "›" >
<!ENTITY euro "€">
]>

XML- . <?xml version="1.0" encoding="windows-1251"?> .

XHML-
XSL- XHTML- ( ). XHTML-, , .

.

XHML- , , :
( ). - , . / .
HTML-:
...




...



...



...



...


XSL-
XSL- :
<xsl:stylesheet version = '1.0' encoding="UTF-8"?>
<xsl:template match="element">

</xsl:template>
</xsl:stylesheet>


: <xsl:stylesheet version = '1.0' encoding="UTF-8"?> - XML- . UTF-8 ( , ).
<xsl:stylesheet> </xsl:stylesheet> - XSL-.
<xsl:template match="element"> </xsl:template> - element.

:
<xsl:template match="element"></xsl:template> - , element . element . <xsl:template match="element" mode="mode1"></xsl:template> - , element mode1 . element . <xsl:template name="template-name"></xsl:template> - template-name . - XML- .
XML- (, XML-, item ), " " :
<xsl:template match="navigation/sections/item"></xsl:template>
, , .., , , , .. navigation , navigation/sections/item sections/item .


, - . , xsl- . .

xsl .

, ( ) :
template_folder - . , my_template . dtd - . . lang - ( ). mod - .
xsl/my_template , layout.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/node()">
</xsl:template>

</xsl:stylesheet>

:
<xsl:template match="/node()"> </xsl:template> - /node() (). /node() //document , .. .

XHTML- <xsl:template match="/node()"></xsl:template>

XML- . , XSL- XML- XHTML- .

, XSL template.xsl (, template - ), :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href=" my_template /layout.xsl"/>
</xsl:stylesheet>

:
<xsl:import href="my_template/layout.xsl"/>
XSL- ( XSL- ) . .


, .. XML- .

- .

, XML- :

<item id="0" parent_id="0" is_published="1" section="1">

/

<item id="1" parent_id="0" is_published="1" section="1" hit="yes">

news




:
id document - id . hit item - , , " ".
, , :
sections - . item - .
, , item item , , :




1. xsl/my_template navigation.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="sections" mode="global_menu">
</xsl:template>

</xsl:stylesheet>


2. layout.xsl :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


3. … layout.xsl :

<xsl:apply-templates select="navigation/sections" mode="global_menu"/>


:
select="navigation/sections" - ( ) - . , navigation/sections .

mode="global_menu" - global_menu . , , , " ", - .

4. , layout.xsl navigation.xsl :
<xsl:import href="navigation.xsl"/>

5. , navigation.xsl , :

<xsl:template match="item" mode="global_menu">
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>


</xsl:template>


:
<xsl:call-template name="href_attribute"/> - . , .. .

<xsl:value-of select="title"/> - - title . @ - .

6. sections :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


:
<xsl:apply-templates select="item" mode="global_menu"/> - item sections . , item item ( sections/item/item ) , .. .

item ( ) . , : <xsl:call-template name="href_attribute"/>

uri- . .

7. ,
, . item :

<xsl:template match="item" mode="global_menu">

<xsl:choose>

<xsl:when test="descendant-or-self::*/@id = /node()/@id">
<xsl:value-of select="title"/>
</xsl:when>

<xsl:otherwise>
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>

</xsl:otherwise>
</xsl:choose>

</xsl:template>


:
<xsl:choose>
<xsl:when></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>

…, , XML- . : <xsl:when test="descendant-or-self::*/@id = /node()/@id">

ID ( document ) ( item ), , .

<xsl:when></xsl:when> , . <xsl:otherwise></xsl:otherwise> - .

8. , href_attribute :

<xsl:template name="href_attribute">
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<xsl:for-each select="ancestor-or-self::item">
<xsl:value-of select="dir"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
</xsl:attribute>
</xsl:template>


xsl:attribute . . a , , href , .. .

<xsl:for-each select="ancestor-or-self::item"> , . ancestor-or-self::item - . dir , .. -.

, - , . , .

UPD:
. , :
parser.proc.ru/iso/xslt-1.zip

( ) Apache.

XML- /xsl/document.xml
- ( ). :
Lang - . . Id - .
<lang_table>
- , .
- :
- ( ):
<item id="0" parent_id="0" is_published="1" section="1">
- . :
Id - . Parent_id - . Is_published - . Dir - uri- . . Section - . .
- .
CMS : :
Html - . , . Com - -. , CMS: , , ..
XSL- , . .

CMS , :
:
Id - .
Container - - ( ).
Sorting - .
Type - :
Com - -
Html - .
Method - .
Title - .
DTD ( ):
<!DOCTYPE site_page [

<!ENTITY nbsp " ">
<!ENTITY sect "§" >
<!ENTITY copy "©">
<!ENTITY laquo "«">
<!ENTITY reg "®">
<!ENTITY deg "°">
<!ENTITY plusmn "±">
<!ENTITY para "¶">
<!ENTITY raquo "»">
<!ENTITY times "×">



<!ENTITY bull "•">
<!ENTITY hellip "…">



<!ENTITY ndash "–">
<!ENTITY mdash "—">
<!ENTITY lsquo "'">
<!ENTITY rsquo "'">
<!ENTITY sbquo "‚">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY bdquo "„">
<!ENTITY lsaquo "‹">
<!ENTITY rsaquo "›" >
<!ENTITY euro "€">
]>

XML- . <?xml version="1.0" encoding="windows-1251"?> .

XHML-
XSL- XHTML- ( ). XHTML-, , .

.

XHML- , , :
( ). - , . / .
HTML-:
...




...



...



...



...


XSL-
XSL- :
<xsl:stylesheet version = '1.0' encoding="UTF-8"?>
<xsl:template match="element">

</xsl:template>
</xsl:stylesheet>


: <xsl:stylesheet version = '1.0' encoding="UTF-8"?> - XML- . UTF-8 ( , ).
<xsl:stylesheet> </xsl:stylesheet> - XSL-.
<xsl:template match="element"> </xsl:template> - element.

:
<xsl:template match="element"></xsl:template> - , element . element . <xsl:template match="element" mode="mode1"></xsl:template> - , element mode1 . element . <xsl:template name="template-name"></xsl:template> - template-name . - XML- .
XML- (, XML-, item ), " " :
<xsl:template match="navigation/sections/item"></xsl:template>
, , .., , , , .. navigation , navigation/sections/item sections/item .


, - . , xsl- . .

xsl .

, ( ) :
template_folder - . , my_template . dtd - . . lang - ( ). mod - .
xsl/my_template , layout.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/node()">
</xsl:template>

</xsl:stylesheet>

:
<xsl:template match="/node()"> </xsl:template> - /node() (). /node() //document , .. .

XHTML- <xsl:template match="/node()"></xsl:template>

XML- . , XSL- XML- XHTML- .

, XSL template.xsl (, template - ), :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href=" my_template /layout.xsl"/>
</xsl:stylesheet>

:
<xsl:import href="my_template/layout.xsl"/>
XSL- ( XSL- ) . .


, .. XML- .

- .

, XML- :

<item id="0" parent_id="0" is_published="1" section="1">

/

<item id="1" parent_id="0" is_published="1" section="1" hit="yes">

news




:
id document - id . hit item - , , " ".
, , :
sections - . item - .
, , item item , , :




1. xsl/my_template navigation.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="sections" mode="global_menu">
</xsl:template>

</xsl:stylesheet>


2. layout.xsl :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


3. … layout.xsl :

<xsl:apply-templates select="navigation/sections" mode="global_menu"/>


:
select="navigation/sections" - ( ) - . , navigation/sections .

mode="global_menu" - global_menu . , , , " ", - .

4. , layout.xsl navigation.xsl :
<xsl:import href="navigation.xsl"/>

5. , navigation.xsl , :

<xsl:template match="item" mode="global_menu">
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>


</xsl:template>


:
<xsl:call-template name="href_attribute"/> - . , .. .

<xsl:value-of select="title"/> - - title . @ - .

6. sections :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


:
<xsl:apply-templates select="item" mode="global_menu"/> - item sections . , item item ( sections/item/item ) , .. .

item ( ) . , : <xsl:call-template name="href_attribute"/>

uri- . .

7. ,
, . item :

<xsl:template match="item" mode="global_menu">

<xsl:choose>

<xsl:when test="descendant-or-self::*/@id = /node()/@id">
<xsl:value-of select="title"/>
</xsl:when>

<xsl:otherwise>
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>

</xsl:otherwise>
</xsl:choose>

</xsl:template>


:
<xsl:choose>
<xsl:when></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>

…, , XML- . : <xsl:when test="descendant-or-self::*/@id = /node()/@id">

ID ( document ) ( item ), , .

<xsl:when></xsl:when> , . <xsl:otherwise></xsl:otherwise> - .

8. , href_attribute :

<xsl:template name="href_attribute">
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<xsl:for-each select="ancestor-or-self::item">
<xsl:value-of select="dir"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
</xsl:attribute>
</xsl:template>


xsl:attribute . . a , , href , .. .

<xsl:for-each select="ancestor-or-self::item"> , . ancestor-or-self::item - . dir , .. -.

, - , . , .

UPD:
. , :
parser.proc.ru/iso/xslt-1.zip

( ) Apache.

XML- /xsl/document.xml
- ( ). :
Lang - . . Id - .
<lang_table>
- , .
- :
- ( ):
<item id="0" parent_id="0" is_published="1" section="1">
- . :
Id - . Parent_id - . Is_published - . Dir - uri- . . Section - . .
- .
CMS : :
Html - . , . Com - -. , CMS: , , ..
XSL- , . .

CMS , :
:
Id - .
Container - - ( ).
Sorting - .
Type - :
Com - -
Html - .
Method - .
Title - .
DTD ( ):
<!DOCTYPE site_page [

<!ENTITY nbsp " ">
<!ENTITY sect "§" >
<!ENTITY copy "©">
<!ENTITY laquo "«">
<!ENTITY reg "®">
<!ENTITY deg "°">
<!ENTITY plusmn "±">
<!ENTITY para "¶">
<!ENTITY raquo "»">
<!ENTITY times "×">



<!ENTITY bull "•">
<!ENTITY hellip "…">



<!ENTITY ndash "–">
<!ENTITY mdash "—">
<!ENTITY lsquo "'">
<!ENTITY rsquo "'">
<!ENTITY sbquo "‚">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY bdquo "„">
<!ENTITY lsaquo "‹">
<!ENTITY rsaquo "›" >
<!ENTITY euro "€">
]>

XML- . <?xml version="1.0" encoding="windows-1251"?> .

XHML-
XSL- XHTML- ( ). XHTML-, , .

.

XHML- , , :
( ). - , . / .
HTML-:
...




...



...



...



...


XSL-
XSL- :
<xsl:stylesheet version = '1.0' encoding="UTF-8"?>
<xsl:template match="element">

</xsl:template>
</xsl:stylesheet>


: <xsl:stylesheet version = '1.0' encoding="UTF-8"?> - XML- . UTF-8 ( , ).
<xsl:stylesheet> </xsl:stylesheet> - XSL-.
<xsl:template match="element"> </xsl:template> - element.

:
<xsl:template match="element"></xsl:template> - , element . element . <xsl:template match="element" mode="mode1"></xsl:template> - , element mode1 . element . <xsl:template name="template-name"></xsl:template> - template-name . - XML- .
XML- (, XML-, item ), " " :
<xsl:template match="navigation/sections/item"></xsl:template>
, , .., , , , .. navigation , navigation/sections/item sections/item .


, - . , xsl- . .

xsl .

, ( ) :
template_folder - . , my_template . dtd - . . lang - ( ). mod - .
xsl/my_template , layout.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/node()">
</xsl:template>

</xsl:stylesheet>

:
<xsl:template match="/node()"> </xsl:template> - /node() (). /node() //document , .. .

XHTML- <xsl:template match="/node()"></xsl:template>

XML- . , XSL- XML- XHTML- .

, XSL template.xsl (, template - ), :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href=" my_template /layout.xsl"/>
</xsl:stylesheet>

:
<xsl:import href="my_template/layout.xsl"/>
XSL- ( XSL- ) . .


, .. XML- .

- .

, XML- :

<item id="0" parent_id="0" is_published="1" section="1">

/

<item id="1" parent_id="0" is_published="1" section="1" hit="yes">

news




:
id document - id . hit item - , , " ".
, , :
sections - . item - .
, , item item , , :




1. xsl/my_template navigation.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="sections" mode="global_menu">
</xsl:template>

</xsl:stylesheet>


2. layout.xsl :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


3. … layout.xsl :

<xsl:apply-templates select="navigation/sections" mode="global_menu"/>


:
select="navigation/sections" - ( ) - . , navigation/sections .

mode="global_menu" - global_menu . , , , " ", - .

4. , layout.xsl navigation.xsl :
<xsl:import href="navigation.xsl"/>

5. , navigation.xsl , :

<xsl:template match="item" mode="global_menu">
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>


</xsl:template>


:
<xsl:call-template name="href_attribute"/> - . , .. .

<xsl:value-of select="title"/> - - title . @ - .

6. sections :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


:
<xsl:apply-templates select="item" mode="global_menu"/> - item sections . , item item ( sections/item/item ) , .. .

item ( ) . , : <xsl:call-template name="href_attribute"/>

uri- . .

7. ,
, . item :

<xsl:template match="item" mode="global_menu">

<xsl:choose>

<xsl:when test="descendant-or-self::*/@id = /node()/@id">
<xsl:value-of select="title"/>
</xsl:when>

<xsl:otherwise>
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>

</xsl:otherwise>
</xsl:choose>

</xsl:template>


:
<xsl:choose>
<xsl:when></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>

…, , XML- . : <xsl:when test="descendant-or-self::*/@id = /node()/@id">

ID ( document ) ( item ), , .

<xsl:when></xsl:when> , . <xsl:otherwise></xsl:otherwise> - .

8. , href_attribute :

<xsl:template name="href_attribute">
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<xsl:for-each select="ancestor-or-self::item">
<xsl:value-of select="dir"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
</xsl:attribute>
</xsl:template>


xsl:attribute . . a , , href , .. .

<xsl:for-each select="ancestor-or-self::item"> , . ancestor-or-self::item - . dir , .. -.

, - , . , .

UPD:
. , :
parser.proc.ru/iso/xslt-1.zip

( ) Apache.

XML- /xsl/document.xml
- ( ). :
Lang - . . Id - .
<lang_table>
- , .
- :
- ( ):
<item id="0" parent_id="0" is_published="1" section="1">
- . :
Id - . Parent_id - . Is_published - . Dir - uri- . . Section - . .
- .
CMS : :
Html - . , . Com - -. , CMS: , , ..
XSL- , . .

CMS , :
:
Id - .
Container - - ( ).
Sorting - .
Type - :
Com - -
Html - .
Method - .
Title - .
DTD ( ):
<!DOCTYPE site_page [

<!ENTITY nbsp " ">
<!ENTITY sect "§" >
<!ENTITY copy "©">
<!ENTITY laquo "«">
<!ENTITY reg "®">
<!ENTITY deg "°">
<!ENTITY plusmn "±">
<!ENTITY para "¶">
<!ENTITY raquo "»">
<!ENTITY times "×">



<!ENTITY bull "•">
<!ENTITY hellip "…">



<!ENTITY ndash "–">
<!ENTITY mdash "—">
<!ENTITY lsquo "'">
<!ENTITY rsquo "'">
<!ENTITY sbquo "‚">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY bdquo "„">
<!ENTITY lsaquo "‹">
<!ENTITY rsaquo "›" >
<!ENTITY euro "€">
]>

XML- . <?xml version="1.0" encoding="windows-1251"?> .

XHML-
XSL- XHTML- ( ). XHTML-, , .

.

XHML- , , :
( ). - , . / .
HTML-:
...




...



...



...



...


XSL-
XSL- :
<xsl:stylesheet version = '1.0' encoding="UTF-8"?>
<xsl:template match="element">

</xsl:template>
</xsl:stylesheet>


: <xsl:stylesheet version = '1.0' encoding="UTF-8"?> - XML- . UTF-8 ( , ).
<xsl:stylesheet> </xsl:stylesheet> - XSL-.
<xsl:template match="element"> </xsl:template> - element.

:
<xsl:template match="element"></xsl:template> - , element . element . <xsl:template match="element" mode="mode1"></xsl:template> - , element mode1 . element . <xsl:template name="template-name"></xsl:template> - template-name . - XML- .
XML- (, XML-, item ), " " :
<xsl:template match="navigation/sections/item"></xsl:template>
, , .., , , , .. navigation , navigation/sections/item sections/item .


, - . , xsl- . .

xsl .

, ( ) :
template_folder - . , my_template . dtd - . . lang - ( ). mod - .
xsl/my_template , layout.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/node()">
</xsl:template>

</xsl:stylesheet>

:
<xsl:template match="/node()"> </xsl:template> - /node() (). /node() //document , .. .

XHTML- <xsl:template match="/node()"></xsl:template>

XML- . , XSL- XML- XHTML- .

, XSL template.xsl (, template - ), :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href=" my_template /layout.xsl"/>
</xsl:stylesheet>

:
<xsl:import href="my_template/layout.xsl"/>
XSL- ( XSL- ) . .


, .. XML- .

- .

, XML- :

<item id="0" parent_id="0" is_published="1" section="1">

/

<item id="1" parent_id="0" is_published="1" section="1" hit="yes">

news




:
id document - id . hit item - , , " ".
, , :
sections - . item - .
, , item item , , :




1. xsl/my_template navigation.xsl :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet SYSTEM "../dtd/entities.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="sections" mode="global_menu">
</xsl:template>

</xsl:stylesheet>


2. layout.xsl :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


3. … layout.xsl :

<xsl:apply-templates select="navigation/sections" mode="global_menu"/>


:
select="navigation/sections" - ( ) - . , navigation/sections .

mode="global_menu" - global_menu . , , , " ", - .

4. , layout.xsl navigation.xsl :
<xsl:import href="navigation.xsl"/>

5. , navigation.xsl , :

<xsl:template match="item" mode="global_menu">
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>


</xsl:template>


:
<xsl:call-template name="href_attribute"/> - . , .. .

<xsl:value-of select="title"/> - - title . @ - .

6. sections :

<xsl:template match="sections" mode="global_menu">

</xsl:template>


:
<xsl:apply-templates select="item" mode="global_menu"/> - item sections . , item item ( sections/item/item ) , .. .

item ( ) . , : <xsl:call-template name="href_attribute"/>

uri- . .

7. ,
, . item :

<xsl:template match="item" mode="global_menu">

<xsl:choose>

<xsl:when test="descendant-or-self::*/@id = /node()/@id">
<xsl:value-of select="title"/>
</xsl:when>

<xsl:otherwise>
<xsl:call-template name="href_attribute"/>
<xsl:value-of select="title"/>

</xsl:otherwise>
</xsl:choose>

</xsl:template>


:
<xsl:choose>
<xsl:when></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>

…, , XML- . : <xsl:when test="descendant-or-self::*/@id = /node()/@id">

ID ( document ) ( item ), , .

<xsl:when></xsl:when> , . <xsl:otherwise></xsl:otherwise> - .

8. , href_attribute :

<xsl:template name="href_attribute">
<xsl:attribute name="href">
<xsl:text>/</xsl:text>
<xsl:for-each select="ancestor-or-self::item">
<xsl:value-of select="dir"/>
<xsl:text>/</xsl:text>
</xsl:for-each>
</xsl:attribute>
</xsl:template>


xsl:attribute . . a , , href , .. .

<xsl:for-each select="ancestor-or-self::item"> , . ancestor-or-self::item - . dir , .. -.

, - , . , .

UPD:
. , :
parser.proc.ru/iso/xslt-1.zip

( ) Apache.

XML- /xsl/document.xml

Source: https://habr.com/ru/post/22236/


All Articles