I.簡単な方法:
この変換:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:org="some:org" exclude-result-prefixes="org">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="org:specs">
<xmp>
<xsl:copy-of select="."/>
</xmp>
</xsl:template>
</xsl:stylesheet>
このXMLドキュメントに適用した場合(提供された整形式ではないテキストが整形式のXMLドキュメントになりました):
<t xmlns:org="some:org">
<org:specs>
<org:wild animal="6" species="land"/>
<org:fish animal="7" species="water"/>
<org:bird animal="8" species="trees"/>
<org:mammal animal="9" species="land"/>
</org:specs>
</t>
生成します:
<xmp>
<org:specs xmlns:org="some:org">
<org:wild animal="6" species="land"/>
<org:fish animal="7" species="water"/>
<org:bird animal="8" species="trees"/>
<org:mammal animal="9" species="land"/>
</org:specs>
</xmp>
そしてこれはブラウザに希望の方法で表示されます:
<org:specs xmlns:org="some:org">
<org:wild animal="6" species="land"/>
<org:fish animal="7" species="water"/>
<org:bird animal="8" species="trees"/>
<org:mammal animal="9" species="land"/>
</org:specs>
II。見栄えの良い、ブラウザで表示されるXML
このアプリケーションがこのような結果を生成する方法については、 XPathVisualizerのXSLTコードを参照してください。
III。ブラウザに必要なすべての入力をテキスト(method="text"
)として生成します。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/*">
<xsl:apply-templates select="*[1]" mode="textify"/>
</xsl:template>
<xsl:template match="*/*[*]" mode="textify">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*" mode="textify"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="*|text()" mode="textify"/>
<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="*/*[not(node())]" mode="textify">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>
<xsl:apply-templates select="@*" mode="textify"/>
<xsl:text>/></xsl:text>
</xsl:template>
<xsl:template match="@*" mode="textify">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="text()" mode="textify">
<xsl:call-template name="textify">
<xsl:with-param name="pText" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="textify">
<xsl:param name="pText"/>
<xsl:if test="string-length($pText) >0">
<xsl:variable name="vChar" select="substring($pText,1,1)"/>
<xsl:choose>
<xsl:when test="$vChar = ' '">
<xsl:value-of select="'&#xA0;'"/>
</xsl:when>
<xsl:when test="$vChar = '	'">
<xsl:value-of select="'&#xA0;&#xA0;'"/>
</xsl:when>
<xsl:when test="$vChar = '
'">
<xsl:value-of select="'<br />'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$vChar"/>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="textify">
<xsl:with-param name="pText" select=
"substring($pText, 2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
この変換を同じXMLドキュメント(上記)に適用すると、目的の結果が生成されます。
<org:specs><br />  <org:wild animal="6" species="land"/><br />  <org:fish animal="7" species="water"/><br />  <org:bird animal="8" species="trees"/><br />  <org:mammal animal="9" species="land"/><br /></org:specs>
そしてそれはブラウザによって次のように表示されます:
<org:specs>
<org:wild animal = "6"species = "land" />
<org:fish animal = "7"species = "water" />
<org:bird animal = "8"species = "trees "/>
<org:mammal animal =" 9 "species =" land "/>
</ org:specs>