Bao gồm XSL bên trong XSL

Nếu XML / XSL Transforms là domian bạn, sau đó có lần khi chúng tôi muốn có một mảnh mã năng động để được sử dụng mục thư viện (được thực hiện tái sử dụng được). Những gì tôi có nghĩa là, có thể có thể được thực hiện rõ ràng hơn với kịch bản ví dụ này.

Hãy tưởng tượng bạn đang tạo một trang web (và sử dụng XML, XSL transfroms ofcourse) và hầu hết các trang sẽ có một Module Bình luận. Vâng! sau đó hoặc là bạn sao chép hoặc dán mã này vào mỗi mẫu trang (mà tôi đã không nói, nhưng làm cho bảo trì và làm lại một cơn ác mộng) hoặc thậm chí tốt hơn, bạn tạo một file INCLUDE có thể được kéo vào có bao giờ bạn muốn nó trong trang của bạn ( s) ...
Vì vậy, LÀM THẾ NÀO ĐỂ CHÚNG TÔI TẠO tập tin XSL INCLUDE và bao gồm nó bên trong một tập tin XSL? Dưới đây là làm thế nào ...

Chỉ cần để làm cho mọi việc rõ ràng ... đây là nhanh chóng danh sách các tập tin mà bạn sẽ tạo ra ... ở đây, chúng tôi sẽ được bao gồm cả thông tin về trái cây và rau quả vào một trang mẹ được gọi là thực phẩm.

1. food.xml - xml dữ liệu tập tin chuyển đổi sẽ được áp dụng
2. food.xsl - chính XSL tập tin, mà sẽ chuyển đổi food.xml của chúng tôi
3. inc_fruits.xsl - XSL bao gồm các tập tin sẽ làm cho trái cây dữ liệu
4. inc_vegtables.xsl - XSL bao gồm các tập tin sẽ làm cho vetetables dữ liệu

Tôi không nghĩ rằng tôi có giải thích nhiều, các mã cho các yếu tố trên, sẽ được tự giải thích ...

FOOD.XML


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<date>July 2008</date>
<description>All about things we eat everyday</description>
<fruits type="tropical">
<item name="mango" moreinfo="http://www.mango.com">Mango is the king of fruits</item>
<item name="banana" moreinfo="http://www.banana.com">Banana once a day , keeps the doctor away</item>
<item name="orange" moreinfo="http://www.orange.com">Orange is the color and rich in vitamin C</item>
<item name="Papaya" moreinfo="http://www.papaya.com">Papaya - Hot when raw, cold when ripe</item>
</fruits>

<vegetables>
<item name="spinach" moreinfo="http://www.spinach.com">Spinach is full of iron</item>
<item name="asparagus" moreinfo="http://www.asparagus.com">Asparagus contains loads of vitamin D </item>
<item name="fenugreek" moreinfo="http://www.fenugreek.com">Fenugreek is rich in vitamin C</item>
</vegetables>
</food>


FOOD.XSL


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="inc_fruits.xsl" />
<xsl:include href="inc_vegetables.xsl" />
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<h3><xsl:value-of select="/food/description" /></h3>
Modification Date : <xsl:value-of select="/food/date" />
<hr/>
<h5> About Fruits</h5>
<xsl:call-template name="about_fruits"/>

<hr/>
<h5> About Vegetables</h5>
<xsl:call-template name="about_vegetables"/>

<hr/>
</ Body>
</ Html>
</ Xsl: template>
</ Xsl: stylesheet>


INC_FRUITS.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_fruits">
<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


INC_VEGETABLES.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_vegetables">
<xsl:for-each select="/food/vegetables/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Tải về tất cả các tập tin trên (249 tải)


Để lại một trả lời

NDK nhà | Bày tỏ IT | Diễn đạt Palate | Diễn đạt Penmenship | Bày tỏ nỗi sợ hãi | Diễn đạt Myself