如何将XML转换为文本

如何将XML转换为文本

本文介绍了如何将XML转换为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接着我之前的问题(如何转换XML?) ,我现在有了一个结构良好的XML文档,如下所示.

Following on from my earlier question (how to transform XML?), I now have a nicely structured XML doc, like this..

<?xml version="1.0" encoding="UTF-8"?>
<root>
<employee id="1" reportsTo="1" title="CEO">
    <employee id="2" reportsTo="1" title="Director of Operations">
        <employee id="3" reportsTo="2" title="Human Resources Manager" />
    </employee>
</employee>
</root>

现在我需要像这样将其转换为javascript.

Now I need to convert it to javascript like this..

var treeData = [
{
"name": "CEO",
"parent": "null",
"children": [
  {
    "name": "Director of Operations",
    "parent": "Top Level",
    "children": [
      {
        "name": "Human Resources Manager",
        "parent": "Level 2: A"
      }
   ]
  }
]
}
];

我已经开始编写XSLT,目前看起来像这样.

I've started writing an XSLT, which currently looks like this..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="root">
    <xsl:apply-templates select="employee" />
</xsl:template>

<xsl:template match="employee">
    <xsl:param name="eId" select="@id" />
    <xsl:param name="eReports" select="@reportsTo" />
    <xsl:param name="eTitle" select="@title" />
    <xsl:value-of select="concat( $eTitle, ' and ', $eId )" />
    <xsl:apply-templates select="employee" />
</xsl:template>

</xsl:stylesheet>

但是当我应用转换(通过pythons lxml库)时,我收到消息无".(以防万一,这是我正在使用的lxml命令...)

But when I apply the transform (via pythons lxml library), I get the message "None".(In case it helps, here's the lxml command I'm using...)

dom = ET.parse("input.xml")
xslt = ET.parse("transform.xslt")
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(ET.tostring(newdom, pretty_print=True))

我知道我的XSLT尚不完善,但是为什么我没有得到任何输出?我至少不应该打印职位名称吗?

I know my XSLT is nowhere near complete, but why aren't I getting any output? Shouldn't I at least be getting the job title printed?

推荐答案

由于OP包含其Python代码,因此已更新.

updated now that OP's included his Python code.

您的问题是lxml.etree.tostring.write方法仅在XML上有意义,而在output method="text"的XSLT结果中却没有意义,而output method="text"可能没有像XML那样的单个根元素.由于某些令人困惑的原因,函数 do 具有一个method=关键字参数,但是它没有任何用处.

Your problem is that lxml.etree.tostring and also the .write method are only meaningful on XML, not on an XSLT result with output method="text" which might not have a single root element like XML does. For some confusing reason, the functions do have a method= keyword argument but it does not do anything useful.

这是您应该做的:

import lxml.etree as etree
data = etree.parse('data.xml')
transform = etree.XSLT(etree.parse('txt.xslt'))
res = transform(data)
bytes(res)
b'\nCEO and 1Director of Operations and 2Human Resources Manager and 3\n'

如果您对真实示例感兴趣,请我最近做了补丁.

If you're interested in a real world example, I recently made a patch.

这篇关于如何将XML转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 23:26