将 xml 文件转换为 xml 的 xslt 模板

xslt template to transform xml file to xml

我正在使用 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>

所需输出 应与源 unicode 字符相同,但在同一文件中具有 sortig uuid。我看到 uuid 排序正常,但此 unicode 正在更改为我不想更改的数字。我

在提问时最好提供一个最小的可重现示例,即 XML/XSLT 对。

请尝试以下概念示例。

我正在使用 SAXON 9.7.0.15

很可能是最后 Python 行导致了问题:

outfile.write(ET.tostring(newdom,pretty_print=True,xml_declaration=True,standalone='yes').decode())

请尝试 Python 最后几行如下:

import sys
if sys.version_info[0] >= 3:
    unicode = str
...
newdom = transform(dom)
infile = unicode((ET.tostring(newdom, pretty_print=True)))
outfile = open(structure + "\" + filename, 'a')
outfile.write(infile, encoding='utf-8', xml_declaration=True, pretty_print=True)

https://lxml.de/api/lxml.etree._ElementTree-class.html#write

参考link:How to transform an XML file using XSLT in Python

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<a:ruleInputTestConfigs xmlns:a="URI">
    <a:value xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:type="xsd:string">あいうえお@domain.com</a:value>
    <a:nameRef>email</a:nameRef>
    <a:id>1</a:id>
</a:ruleInputTestConfigs>

XSLT

<?xml version="1.0"?>
<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:stylesheet>

输出XML

<?xml version="1.0" encoding="UTF-8"?>
<a:ruleInputTestConfigs xmlns:a="URI">
    <a:value xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">あいうえお@domain.com</a:value>
    <a:nameRef>email</a:nameRef>
    <a:id>1</a:id>
</a:ruleInputTestConfigs>