【问题标题】:Python Transforming XML using XSLTPython 使用 XSLT 转换 XML
【发布时间】:2014-04-29 01:58:32
【问题描述】:

我是 python 新手,一直在尝试使用 XSLT 转换 XML 文件。 这是我自己想出来的一个python脚本,generate_ioc.py

import os
import sys
import time
import shutil
import logging
import Queue
import ftplib
import subprocess
import re
import lxml.etree as ET
try:
    report_path = os.path.join("/home",
                "user",
                "Desktop",
                "stix_to_openioc")
stix_report = os.path.join(report_path, "report.stix.xml")
print("Retrieve STIX report successful")

# Sanitize report (oxb, remove xlms)
with open (stix_report, "r") as stix_file:
    stix_xml = stix_file.read()
stix_xml = re.sub(u"xmlns='https://github.com/STIXProject/schemas/blob/master/stix_core.xsd' ", u"", stix_xml)
RE_XML_ILLEGAL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \
             u'|' + \
             u'([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \
              (unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
               unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
               unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff))
stix_xml = re.sub(RE_XML_ILLEGAL, "?", stix_xml)

print("Sanitize STIX report successful")

# Save sanitized report to file
tree = ET.XML(stix_xml)
with open(stix_report, "w") as stix_file:
    stix_file.write(ET.tostring(tree))
print("Save STIX report successful")

# Get xsl file
    xslt_path = os.path.join("/home",
                "user",
                "Desktop",
                "stix_to_openioc",
                                "stix_to_openioc.xsl")
print("Retrieve XSL file successful")

#Perform xsl tranformation
#dom = ET.parse(stix_report)
#xslt = ET.parse(xslt_path)
#transform = ET.XSLT(xslt)
#newdom = transform(dom)
#xslt_xml = ET.tostring(newdom, pretty_print=True)
#print("XSL transformation successful")
#print(xslt_xml)
#I have tried this ^ but it resulted in the same error 

from lxml import etree
f_xsl = 'stix_to_openioc.xsl'
f_xml = 'report.stix.xml'
f_out = 'report.ioc.xml'

transform = etree.XSLT(etree.parse(f_xsl))
result = transform(etree.parse(f_xml))
result.write(f_out)

# Get new stix file
openioc_report = os.path.join(report_path,
                "report.openioc.xml")
print("Retrieve OpenIOC report successful")

# Save stix report
with open(openioc_report, "w") as openioc_file:
    openioc_file.write(xslt_xml)
print("Save OpenIOC report successful")

except OSError as e:
log.warning("Error accessing stix report (task=%d): %s", self.task.id, e)

错误代码:

/usr/bin/python2 /home/user/Desktop/IOC/generate_ioc.py
Retrieve STIX report successful
Sanitize STIX report successful
Save STIX report successful
Retrieve XSL file successful
Traceback (most recent call last):
File "/home/user/Desktop/IOC/generate_ioc.py", line 53, in <module>
transform = ET.XSLT(xslt)
File "xslt.pxi", line 403, in lxml.etree.XSLT.__init__ (src/lxml/lxml.etree.c:122894)
lxml.etree.XSLTParseError: xsltParseStylesheetProcess : document is not a stylesheet

Process finished with exit code 1

我想知道我出了什么问题导致了这种情况。 注意:我对 python 和 XML 很陌生,所以任何建议都将不胜感激,我愿意学习并花时间纠正这个错误。注意 2:stix_to_openioc.xsl 存在于正确的目录中。

【问题讨论】:

    标签: python xml xslt


    【解决方案1】:

    您对 xml 进行了一些清理并将其保存到路径字符串为 stix_report 的文件中:

    with open(stix_report, "w") as stix_file:
        stix_file.write(ET.tostring(tree))
    print("Save STIX report successful")
    

    然后你创建一个 xslt 文件的字符串路径:

    xslt_path = os.path.join("/home",
                "user",
                "Desktop",
                "stix_to_openioc",
                "stix_to_openioc.xsl")
    print("Retrieve XSL file successful")
    

    然后你从新变量加载 xml 和 xsl 文件:

    from lxml import etree
    f_xsl = 'stix_to_openioc.xsl'
    f_xml = 'report.stix.xml'
    f_out = 'report.ioc.xml'
    
    transform = etree.XSLT(etree.parse(f_xsl))
    result = transform(etree.parse(f_xml))
    result.write(f_out)
    

    我不能保证它会起作用(因为我不知道这些文件中有什么),但我认为更改此代码是一个好的开始:

    from lxml import etree
    f_xsl = xslt_path
    f_xml = stix_report
    f_out = 'report.ioc.xml'
    
    transform = etree.XSLT(etree.parse(f_xsl))
    result = transform(etree.parse(f_xml))
    result.write(f_out)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多