Python 4Suite-XMLを使用してxml+xsltを混ぜ合わせる



Pyanaがどうしても動かせなかったので、4Suite-XMLを
試してみることにします。

4Suite-XML


easy_install 4Suite-XML


これでインストールできますが、どうやらftpサイトでしか
モジュールを公開していないようなので、httpでの
接続しか許可されていない環境ではモジュールから
インストールする必要があります。


#wget http://downloads.sourceforge.net/foursuite/4Suite-XML-1.0.2.tar.gz
#tar zxvf 4Suite-XML-1.0.2.tar.gz
#cd 4Suite-XML-1.0.2
#python setup.py install

4Suite-XML version 1.0.2 has been successfully installed!

Python modules (including C extensions)
  /usr/local/lib/python2.5/site-packages

Executable scripts (for PATH environment variable)
  /usr/local/bin

Examples, demos and other miscellaneous data files
  /usr/local/share/4Suite

Developer files (regression tests)
  /usr/local/lib/4Suite

Text documentation
  /usr/local/share/doc/4Suite

HTML documentation
  /usr/local/share/doc/4Suite/html



あっさりインストールできました。
・・・Pyanaの苦労はいったい何だったのか。


早速、
/usr/local/share/doc/4Suite/html/CoreManual.html
6.3 Exampleに掲載されているソースを実行してみます。



#vi test.py

# The identity transform: duplicates the input to output
TRANSFORM = """
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

</xsl:stylesheet>"""

SOURCE = """<spam id="eggs">I don't like spam</spam>"""

# The processor class is the core of the XSLT API
from Ft.Xml.Xslt import Processor
processor = Processor.Processor()

# We use the InputSource architecture
from Ft.Xml import InputSource

# Prepare an InputSource for the transform
transform = InputSource.DefaultFactory.fromString(TRANSFORM,
  "http://spam.com/identity.xslt")

# Prepare an InputSource for the source document
source = InputSource.DefaultFactory.fromString(SOURCE,
  "http://spam.com/doc.xml")
processor.appendStylesheet(transform)
result = processor.run(source)

# result is a string with the serialized transform result
print result


※作成したプログラムを実行
# python test.py
<?xml version="1.0" encoding="UTF-8"?>
<spam id="eggs">I don't like spam</spam>



動いている模様です。



もどる