【问题标题】:xslt template to transform xml file to xml用于将 xml 文件转换为 xml 的 xslt 模板
【发布时间】: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


【解决方案1】:

在提出问题时,最好提供一个可重现的最小示例,即 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

参考链接: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>

【讨论】:

  • 我尝试将其更改为 UTF-16,但我仍然看到它将 unicode 更改为数字。
  • @sandy,你看到我关于最小可重现示例的评论了吗?
  • @sandy,我也更新了我的答案。看看这个。您仍然没有提供最小的可重现示例:##1-4。
  • 它是一个很大的 xml 文件,我无法将其粘贴到此处。对于 4. 它已经在 .xslt 模板中 xsl:stylesheet version="1.0
  • @sandy。我们不需要整个 XML 文件。但是我们确实需要它的序言和一个根元素。将其称为 MINIMAL 可重现示例是有原因的。
猜你喜欢
  • 2019-04-11
  • 2014-11-15
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2019-12-15
  • 1970-01-01
相关资源
最近更新 更多