XSLTãªãã¦ä¹ ãã¶ãã«æ¸ãã
2ã¡ãããå¾å¾ãã¦ãããããã¾ãã¾ãããªã®è¦ã¤ããã
XML+XSLTã§ã¤ã³ã©ã¤ã³è¦ç´ ã£ã¦ã©ããã£ã¦å®ç¾ããã®ï¼ ä¾ãã°ã <top> <block> ããã¹ã<link url="http://example.jp">ãªã³ã¯</link> </block> </top> ã¨ããXMLããã£ãã¨ãã«XSLTå´ã¯ã©ãããã° <p> ããã¹ã<a href="http://example.jp">ãªã³ã¯</a> </p> ã®ããã«ã§ããã®ï¼ <top> <block> <text>ããã¹ã</text><link url="http://example.jp">ãªã³ã¯</link> </block> </top> ã¨ã¯æ¸ããããªããã ãã©ãXML使いのスレ2.0
ãã¯ãæã®èªåãè¦ã¦ãããã w
確ã3å¹´ã®é ã ã£ãããåããããªåé¡ã«ã¯ã¾ã£ã¦ããã
XSLTãªãã¦ãã以æ¥ã ãã©ãé©å½ã«æ¸ãã¦ã¿ãã
ã¾ãã対象ã¨ãªãXMLã¯ãããªæãã
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet href="style.xsl" type="text/xsl"?> <page title="test"> <link url="http://www.google.co.jp">ãªã³ã¯</link>ã¨ãã ç»å<img url="http://hogehost/image/image.png" alt="hoge" w="80" h="15"/>ã¨ãã ããã¼ã¨ã<banner base="http://hogehost/" img="image/image.png" link="index.html" alt="hoge" w="80" h="15"/>ã<br/> æ¹è¡ã¨ãã<strong>強調</strong>ã¨ãã </page>
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="page"> <html> <head> <title><xsl:value-of select="@title"/></title> </head> <body><p> <xsl:call-template name="body"/> </p></body> </html> </xsl:template> <!-- ãã¼ã¸æ¬ä½é¨åã®ãã³ãã¬ã¼ã --> <xsl:template name="body"> <!-- åè¦ç´ ã®ååã«ãã£ã¦é©ç¨ãããã³ãã¬ã¼ããåå² --> <xsl:for-each select="node()"> <xsl:choose> <xsl:when test="name(.)='link'"> <xsl:call-template name="link"/> </xsl:when> <xsl:when test="name(.)='img'"> <xsl:call-template name="img"/> </xsl:when> <xsl:when test="name(.)='banner'"> <xsl:call-template name="banner"/> </xsl:when> <xsl:when test="name(.)='strong'"> <xsl:call-template name="strong"/> </xsl:when> <xsl:when test="name(.)='br'"> <br/> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> <!-- ãã¤ãã¼ãªã³ã¯ã®ãã³ãã¬ã¼ã --> <xsl:template name="link"> <a> <xsl:attribute name="href"> <xsl:value-of select="@url"/> </xsl:attribute> <xsl:value-of select="."/> </a> </xsl:template> <!-- ç»åã®ãã³ãã¬ã¼ã --> <xsl:template name="img"> <img> <xsl:attribute name="src"> <xsl:value-of select="@url"/> </xsl:attribute> <xsl:attribute name="alt"> <xsl:value-of select="@alt"/> </xsl:attribute> <xsl:attribute name="width"> <xsl:value-of select="@w"/> </xsl:attribute> <xsl:attribute name="height"> <xsl:value-of select="@h"/> </xsl:attribute> </img> </xsl:template> <!-- ããã¼(ãªã³ã¯+ç»å)ã®ãã³ãã¬ã¼ã --> <xsl:template name="banner"> <a> <xsl:attribute name="href"> <xsl:value-of select="@base"/><xsl:value-of select="@link"/> </xsl:attribute> <img> <xsl:attribute name="src"> <xsl:value-of select="@base"/><xsl:value-of select="@img"/> </xsl:attribute> <xsl:attribute name="alt"> <xsl:value-of select="@alt"/> </xsl:attribute> <xsl:attribute name="width"> <xsl:value-of select="@w"/> </xsl:attribute> <xsl:attribute name="height"> <xsl:value-of select="@h"/> </xsl:attribute> </img> </a> </xsl:template> <!-- ããã¹ãã強調表示ããé¨åã®ãã³ãã¬ã¼ã --> <xsl:template name="strong"> <strong> <xsl:value-of select="."/> </strong> </xsl:template> </xsl:stylesheet>
ãã®XSLTã§ãæåã®XMLããããªæãã«å¤æãããã
<html> <head> <title>test</title> </head> <body><p> <a href="http://www.google.co.jp">ãªã³ã¯</a>ã¨ãã ç»å<img src="http://hogehost/image/image.png" alt="hoge" width="80" height="15"/>ã ããã¼ã¨ã<a href="http://hogehost/index.html"> <img src="http://hogehost/image/image.png" alt="hoge" width="80" height="15"/> </a>ã<br/> æ¹è¡ã¨ãã<strong>強調</strong>ã¨ãã </p></body> </html>
ããããã¨ææãã ãã©ãä¸å¿åãã
èå¿ãªé¨åã¯ãbodyãã³ãã¬ã¼ãã§ãnodeé¢æ°ä½¿ã£ã¦åè¦ç´ ãå
¨é¨åå¾ããã®ä¸ã¤ä¸ã¤ã®è¦ç´ ã«é©ç¨ãã¹ããã³ãã¬ã¼ããé¸ãã§ããã
ãã®ãã³ãã¬ã¼ãã¯matchå±æ§ãããªãã¦nameå±æ§ã使ã£ã¦ããããpageè¦ç´ 以å¤ã«ãããã¯è¦ç´ ãä½ã£ãã¨ãã¦ããããããå¼ã³åºãã°åãã»ã»ã»ã¯ãã
XSLTãé¢ç½ãã¯ãããã ãã©ããªãã¨è¨ãããéæãã ãããããã°ã£ã¦XSLTæ¸ããã¨ããã§ç¬èªè¦æ ¼ã§ããç¡ãããã ãã
ãã®è³ªåè
ã¯ããããæ´ã«DTDããXML Schemaããããã ãã¨äºè¨w