LoginSignup
7
8

More than 3 years have passed since last update.

【XSLT】XSL入門

Last updated at Posted at 2019-07-02

XSLとは

参考記事によると

XSLとは,XML文書の構造を別の形式に変換するためのルールを記述することができる言語
異なる構造のXML文書に変換したり、HTMLやCSVなど別のデータ形式に変換したり、データの一部の置換や移動などを行なうことができる

超ざっくりとしたイメージ↓

XML --> SXLで変換 --> XML(XHTML)

とにかく実行してみよう

スクリーンショット 2019-07-02 17.15.17.png

左がXSL,右がXML
スクリーンショット 2019-07-03 9.09.14.png
XMLファイルをFireFoxで開く
スクリーンショット 2019-07-02 17.16.09.png

XMLがXSLによって変換されました.

コード

同ディレクトリ内に置いて実行してみてください

cdcatalog.xsl
<?xml version="1.0" encoding="UTF-8"?>
<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>Title</th>
      <th>Artist</th>
      <th>Country</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
      <td><xsl:value-of select="country"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
cdcatalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>One Time</title>
    <artist>Justin Bieber</artist>
    <country>Canada</country>
  </cd>
  <cd>
    <title>My heart will go on</title>
    <artist>Céline Dion</artist>
    <country>Canada</country>
  </cd>
  <cd>
    <title>We Are Never Ever Getting Back Together</title>
    <artist>Taylor Swift</artist>
    <country>USA</country>
  </cd>
</catalog>

ちなみに,Google Chrome だとうまく表示されないです.

XSL動かす方法は他にもありますが

FireFoxとっちゃうのが一番手取り早いです.

XPath

XPathの説明はこのページが超わかりやすいです

GitHub

参考・引用

https://www.w3schools.com/xml/xsl_transformation.asp
http://e-words.jp/w/XSL.html
https://takuya-1st.hatenablog.jp/entry/2015/08/31/120000
https://webbibouroku.com/Blog/Article/xpath

7
8
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8