1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XSLTでXMLの要素・属性をコピーする

Last updated at Posted at 2018-01-23

XSL

test.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:x="http://www.w3.org/1999/xhtml"
  >

  <xsl:output method="xml" encoding="utf-8" indent="yes" />

  <!-- 要素・属性をコピー(空要素タグが展開されない) -->
  <xsl:template match="x:hr|x:br">
    <xsl:copy-of select="."/>
  </xsl:template>

  <!-- 要素・属性をコピー -->
  <xsl:template match="/|node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
<!--
msxsl test_in.xml test.xsl -o test_out.xml
-->

空要素タグのうち、hrタグとbrタグは、xsl:copy-ofでコピーする。
metaタグは、xsl:copyでコピーする。

XML(in)

test_in.xml
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<html xml:lang="jp" lang="jp" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XSLTメモ (001)</title>
</head>
<body>
<h1>XSLTでXMLの要素・属性をコピーする</h1>
<p style="color:red;">XSLTでXMLの要素・属性をコピーする</p>
<p>XSLTでXMLの<br/>
<b>空要素タグ</b>
<br/>をコピーする</p>
<hr/>
</body>
</html>

xsl:copy-ofでコピーされたhrタグとbrタグは、展開されていない。
xsl:copyでコピーされたmetaタグは展開されている。
※今のところ、msxslでのみ確認

XML(out)

test_out.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<html xml:lang="jp" lang="jp" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</meta>
<title>XSLTメモ (001)</title>
</head>
<body>
<h1>XSLTでXMLの要素・属性をコピーする</h1>
<p style="color:red;">XSLTでXMLの要素・属性をコピーする</p>
<p>XSLTでXMLの<br />
<b>空要素タグ</b>
<br />をコピーする</p>
<hr />
</body>
</html>
1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?