アプリ版:「スタンプのみでお礼する」機能のリリースについて

値がないXML文書に、
<exsample />
こう書かれてしまうものを、
<exsample></exsample>
このように出力したいのですが、出来ずでした。
以下は、私のやり方です。

■ XSL
<?xml version="1.0" encoding="Shift_JIS"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml" encoding="Shift_JIS" indent="yes" xalan:indent-amount="4"/>
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

■ XML
<?xml version="1.0" encoding="Shift_JIS"?>
<root>
<section>
<category>
<exsample1>value</exsample1>
<exsample />
</category>
</section>
</root>

ご教授、よろしくお願いいたします。

A 回答 (1件)

output method="xml" だと <element></element> は <element/> と等価だから xslt からは制御はできないよ?



そういう出力を強制するオプションを持つ処理系を使うとか。

無理やりやるとしたら xsl:comment かな?
処理系によるけど。。。

■ 検証用ファイル
[q7574909.xml]
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <textnode>text</textnode>
  <commentnode><!--comment--></commentnode>
  <emptynode />
</root>


[q7574909.xsl]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8"/>

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

<xsl:template match="@*"><xsl:copy/></xsl:template>

<xsl:template match="node()">
  <xsl:copy>
    <xsl:choose>
      <xsl:when test="0!=count(node())">
        <xsl:apply-templates select="@*|node()"/>
      </xsl:when>
      <xsl:otherwise><xsl:comment></xsl:comment></xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>


■ 結果(Xalan)

C:\xalan-j_2_7_1>java -cp xalan.jar;serializer.jar;xml-apis.jar;xercesImpl.jar org.apache.xalan.xslt.Process -IN q7574909.xml -XSL q7574909.xsl
<?xml version="1.0" encoding="UTF-8"?><root>
  <textnode>text</textnode>
  <commentnode><!--comment--></commentnode>
  <emptynode><!----></emptynode>
</root>
C:\xalan-j_2_7_1>

■ 結果(xsltproc)

$ xsltproc q7574909.xsl q7574909.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <textnode>text</textnode>
  <commentnode><!--comment--></commentnode>
  <emptynode></emptynode>
</root>
$
    • good
    • 0
この回答へのお礼

回答いただきありがとうございます。こういうやり方もあるんですね。
例もいただいて直観的でした。ありがとうございます!

お礼日時:2012/07/07 09:20

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