xmlのデータを変換せずにそのまま出力する
XSLT変換で、xmlのデータをそのまま全部取得したいときにどのようなコードを書けばよいか記載。
それにプラスアルファで、要素を追加したり削除したりするのは別記事にて記載。
ソースコード
xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
<head>
<title name="タイトル">従業員表</title>
</head>
<data>
<item itemid="1">名前</item>
<item itemid="2">年齢</item>
<item itemid="3">部署</item>
</data>
</root>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
変換結果
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
<head>
<title name="タイトル">従業員表</title>
</head>
<data>
<item itemid="1">名前</item>
<item itemid="2">年齢</item>
<item itemid="3">部署</item>
</data>
</root>
解説
変換しないで抽出している箇所は下記。
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
@*は全ての属性ノード、node()は属性ノード以外を除く全てのノードなので、
これをノード集合の和として使う”|”をつかって、全てのノードをコピーしている。

