公式アカウントからの投稿が始まります

たとえば、
<root>
<test>
<a>あああああああああああああ<bold>いいいい</bold>あああああああ</a>
<test>
<test>
<a>ううううううう<bold>えええええ</bold>ううううう</a>
<test>
</root>
こんなxmlがあった場合、
<bold>ではさまれた部分を
htmlの<b>に置き換えることは可能なのでしょうか。
ただしちゃんと<a>の要素の中としてです。
<bold>部分だけ取り出してではなく。
つまり、
<p>あああああああああああああ<b>いいいい</b>あああああああ</p>
<p>ううううううう<b>えええええ</b>ううううう</p>
みたいな出力を得たいです。

A 回答 (3件)

=========source.xml=============


<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
<a>あああああああああああああ<bold>い<strong>い</strong>いい</bold>あああああああ</a>
</test>
<test>
<a>ううううううう<bold>えええええ</bold>ううううう</a>
</test>
</root>
=============transform.xsl===============
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="test">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>

<xsl:template match="bold">
<xsl:element name="b">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="a">
<xsl:element name="p">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
====================result.xml===================
<?xml version="1.0" encoding="UTF-16"?>
<root>
<p>あああああああああああああ<b>い<strong>い</strong>いい</b>あああああああ</p>
<p>ううううううう<b>えええええ</b>ううううう</p>
</root>
    • good
    • 0

補足。


>#1さん
もとのXMLファイルが

<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
<a>あああああああああああああ<bold width="15px">いbold<strong>い</strong>いい</bold>あああああああ</a>
</test>
<test>
<a>ううううううう<bold>えええええ</bold>ううううう</a>
</test>
</root>

だったら?
【bold要素の中のテキスト『いbold』の『bold』は変換してはいけない】
ちなみに俺の奴の場合,
<bold width="15px">いbold<strong>い</strong>いい</bold>
→<b width="15px">いbold<strong>い</strong>いい</b>
と、属性はそのまま保持される。
(単純に<bold>を<b>に変換したものとは異なる。まあこの程度の変換なら正規表現なんかを使って出来るかもしれないけどね。)

#各XML,XSLTにおいて名前空間に関する検討はしていないので注意。
#あと、HTMLにおいてb要素にはたぶんwidth属性がない、ということや、Strictでないソースだということは理解しています。
    • good
    • 0

boldという文字列を、bに置き換えれば良いだけの話では?



メモ帳なら
[表示]→[置換]で出来ます。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!