要素テキストのスペースを正規化しながら、そこにある可能性のあるコメントを保持する方法はありますか?
現時点では、スペースとコメントをフィルタリングしていますか?
前もって感謝します
はい、次のようなことができます。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()" priority="-1">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>
</xsl:stylesheet>
この入力で実行すると:
<root>
<mynode>
here is some text <!-- That is some text -->
and here is some other text
<!-- That was some other text -->
</mynode>
<!-- Here are some more comments -->
</root>
結果は次のとおりです。
<root><mynode>here is some text<!-- That is some text -->and here is some other text<!-- That was some other text --></mynode><!-- Here are some more comments --></root>
これを実際に何かを行う XSLT に統合する方法を示すには、次のようにします。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()" priority="-1">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>
<xsl:template match="/">
<myNewRoot>
<myNewNode>
<xsl:apply-templates select="root/mynode/node()" />
</myNewNode>
</myNewRoot>
</xsl:template>
</xsl:stylesheet>
これを上記の入力で実行すると、結果は次のようになります。
<myNewRoot>
<myNewNode>here is some text<!-- That is some text -->and here is some other text<!-- That was some other text --></myNewNode>
</myNewRoot>