HTMLへの変換
HTMLに変換すると、埋め込みWebkitやMozEmbedなどのHTMLベースのレンダラーでArkMLを簡単に表示できます。
XSLT 1.0
XSLTはユニバーサルXML変換言語であり、言語固有の手続き型変換を優先して、すべてのプログラミング環境で使用する必要があります。
この変換にはXSLT2.0機能は必要ありません。
注:XSLTはXMLでのみ動作します。 最初にSGMLをXMLに変換する必要があります。これは一般的に単純なプロセスです。コマンドライン環境については、「osx」ユーティリティを参照してください。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="RichColor"> <span> <xsl:apply-templates select="@* | node()"/> </span> </xsl:template> <xsl:template match="RichColor/@Color"> <xsl:attribute name="style"> <xsl:value-of select="concat( 'color: rgba(', round(substring-before(., ',') * 255), ', ', round(substring-before(substring-after(., ','), ',') * 255), ', ', round(substring-before(substring-after(substring-after(., ','), ','), ',') * 255), ', ', substring-after(substring-after(substring-after(., ','), ','), ',') * 1, ');' )"/> </xsl:attribute> </xsl:template> <xsl:template match="text()" name="br"> <xsl:param name="str" select="."/> <xsl:choose> <xsl:when test="not(contains($str, '
'))"> <xsl:copy-of select="$str"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring-before($str, '
')"/> <br /> <xsl:call-template name="br"> <xsl:with-param name="str" select="substring-after($str, '
')"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
LXMLとBeautifulSoupを使用したPython3
lxmlとbeautifulsoup4の2つのPyPIパッケージが必要になります。
まず、ArkMLを解析するたびにXSLTトランスフォーマーが作成されないように、XSLTトランスフォーマーを作成する必要があります。「xsltstr」は、上記のXSLTコードを含む文字列(またはバイト)です。
from lxml import etree transform = etree.XSLT(etree.fromstring(xsltstr))
次に、ArkMLデータを解析します。 arkmlstr」は、入力ArkMLデータを含む文字列(またはバイト)です。 XSLTはXMLドキュメントでのみ動作するため、入力はスパン要素でラップする必要があります(XMLフラグメントにXSLT変換を適用できるAPIは、フラグメントの周囲に何もドキュメントを密かに追加しませんが、Pythonでは明示的に行われます)。 出力( "htmlspan")は、HTMLspan要素を含むLXMLオブジェクトになります。 このコードは通常、Pythonインストールにインストールされている使用可能なSGMLパーサーを自動的に選択することによってSGMLを解析します。
from lxml.html import soupparser htmlspan = transform(soupparser.fromstring(b"<span>" + arkmlstr.replace(b'&', b'&') + b"</span>", features="xml")[0])
そして、それをバイト/文字列に戻すことができます:
print(b''.join([etree.tostring(c) for c in htmlspan.getroot().iterdescendants()]))