【发布时间】:2021-08-12 21:56:18
【问题描述】:
我使用python代码解析多个.xml文件
import os
import lxml.etree as ET
import sys
inputpath =
xsltfile =
outpath =
dir = []
if sys.version_info[0] >= 3:
unicode = str
for dirpath, dirnames, filenames in os.walk(inputpath):
structure = os.path.join(outpath, dirpath[len(inputpath):])
if not os.path.isdir(structure):
os.mkdir(structure)
for filename in filenames:
if filename.endswith(('.xml')):
dir = os.path.join(dirpath, filename)
print(dir)
dom = ET.parse(dir)
xslt = ET.parse(xsltfile)
transform = ET.XSLT(xslt)
newdom = transform(dom)
infile = unicode((ET.tostring(newdom, pretty_print=True,xml_declaration=True,standalone='yes')))
outfile = open(structure + "\\" + filename, 'a')
outfile.write(infile)
我确实有一个 .xslt 模板,用于对同一文件中的 uuid 进行排序。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" standalone="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="uuids">
<xsl:copy>
<xsl:apply-templates select="uuid">
<xsl:sort select="."/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Desired Output 应与源 unicode char 相同,但 sortig uuid 位于同一文件中。我看到 uuid 的排序很好,但是这个 unicode 正在更改为我不想更改的数字。我
【问题讨论】:
-
您在输入 XML 中是否有带有 encoding 的 XML prolog 声明?
-
XSLT - 在提出问题时,您需要提供最小可重现示例:(1) 输入 XML。 (2) 你的逻辑,以及试图实现它的 XSLT。 (3) 期望的输出。 (4) XSLT 处理器及其版本。
-
我认为您应该为您的问题添加一个
python标记,因为问题不在于您的 XSLT 代码,而在于您的调用应用程序对 XSL 转换的输出进行序列化的方式。跨度>
标签: python xml xslt unicode utf-8