Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Вебмастеру (http://forum.oszone.net/forumdisplay.php?f=22)
-   -   [решено] Как вывести XML аттрибуты через XSL (http://forum.oszone.net/showthread.php?t=74238)

hasherfrog 08-11-2006 15:33 508705

Как вывести XML аттрибуты через XSL
 
Пишем test.xsl:
PHP код:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" standalone="yes"/>

<xsl:template match="*">
<html><head></head><body>
<xsl:apply-templates mode="line"/>
</body></html>
</xsl:template>

<xsl:template match="*" mode="line">
<div>
<xsl:call-template name="graft"/>
<xsl:apply-templates select="." mode="item"/>
<xsl:apply-templates mode="line"/>
</div>
</xsl:template>

<xsl:template match="*" mode="item">
<xsl:value-of select="name()"/>
</xsl:template>

<xsl:template name="graft">
<xsl:apply-templates select="ancestor::*" mode="tree"/>
&#хA0;
</xsl:template>

<xsl:template match="*" mode="tree">
&#хA0;
</xsl:template>

</xsl:stylesheet>

Вот такой XSL позволяет вывести псевдодеревцем любой XML документ.
Но атрибуты не выводятся.
Например, пишем test.xml:
Код:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<something a="a" b="a">
<c/>
</something>
</root>

при его выводе, получим
Код:

something
      c

А где "а" и "b"?
В теле они есть, но их не видно. Как сделать, чтобы их было видно?

hasherfrog 08-11-2006 16:06 508717

Всё, спасиб, решил.
PHP код:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" standalone="yes"/>

<xsl:template match="*">
<html><head></head><body>
<xsl:apply-templates mode="line"/>
</body></html>
</xsl:template>

<xsl:template match="*" mode="line">
<div>
<xsl:call-template name="graft"/>
<xsl:apply-templates select="." mode="item"/>
 <xsl:for-each select="@*">
<div>
 <xsl:call-template name="graft"/>
 <i>
 <xsl:value-of select="name()"/>
 <xsl:text> (attribute) </xsl:text>
 </i>
</div>
 </xsl:for-each>
<xsl:apply-templates mode="line"/>
</div>
</xsl:template>

<xsl:template match="*" mode="item">
<b><xsl:value-of select="name()"/></b>
</xsl:template>

<xsl:template name="graft">
<xsl:apply-templates select="ancestor::*" mode="tree"/>
&#хA0;
</xsl:template>

<xsl:template match="*" mode="tree">
&#хA0;
</xsl:template>

</xsl:stylesheet>



Время: 00:49.

Время: 00:49.
© OSzone.net 2001-