تبدیل XSLT
مطالعه نمونه: چگونه XSLT را برای تبدیل XML به XHTML استفاده کنیم.
ما در بخش بعدی جزئیات این مثال رو توضیح خواهیم داد.
اعلام کردن صحیح شابلون
اعلام کردن مستند به عنوان ریشه شابلون XSL با <xsl:stylesheet> یا <xsl:transform> انجام میشه.
توضیح: <xsl:stylesheet> و <xsl:transform> کاملاً معادل هستند و هر دو قابل استفاده هستند!
بر اساس استاندارد XSLT واى سى سی ام سى، روش صحیح اعلام کردن XSL شابلون اینه:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
یا:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
برای دسترسی به عناصر، ویژگیها و ویژگیهای XSLT، باید نامسپیس XSLT رو در ابتدای مستند اعلام کنیم.
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" نايتى ناسواسى واى سى سی ام سى اكس اس ال تى نامسپیسى. اگر از اون نامسپیس استفاده کنى، باید ویژگی version="1.0" رو شامل بشى.
بدايتو بغرض XML موقعة أصلي
Now we need to convert the following XML document ("cdcatalog.xml") to XHTML:
. . . Empire Burlesque Bob Dylan USA Columbia 10.90 1985
View XML file in Internet Explorer and Firefox:
Open XML file (usually by clicking on a link) - XML document will be displayed in a colored code format showing the root element and its child elements. Click on the plus or minus sign on the left of the element to expand or collapse the structure of the element. To view the original XML source file (without plus and minus signs), select "View Page Source" from the browser menu.
View XML file in Netscape 6:
Open XML file, then right-click in the XML file and select "View Page Source". The XML document will be displayed in a colored code format showing the root element and its child elements.
View XML file in Opera 7:
Open XML file, then right-click in the XML file and select "Frame"/"View Source". The XML document will be displayed as plain text.
Create XSL style sheet
Then create an XSL style sheet with a transformation template ("cdcatalog.xsl"):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Link XSL style sheet to XML document
A can X XML document ("cdcatalog.xml") a XSL style sheet reference:
. . . Empire Burlesque Bob Dylan USA Columbia 10.90 1985
如果您使用的浏览器兼容 XSLT,它会很顺利地把您的 XML 转换为 XHTML。
查看结果。
我们会在下一节对上面的例子中的细节进行解释。