python - Python,如何获取 xsl 样式表的参数?
问题描述
我想获取xsl
用于将文件转换为xml
文件的csv
文件的参数。我特别想得到这条线:
<xsl:param name="sep" select="','"/>
我试过的:
with open(file, "r") as file:
content = file.readlines()
regex = re.compile(r"""<xsl:param +name *= *"[0-9A-Za-z]+" +select *= *"\\'.\\'"/>""")
for line in content:
print(line)
match = regex.match(line)
if match:
pass
# do something
我尝试了不同的正则表达式,但没有任何效果。我正在使用 python 3.6 并lxml
对其进行转换。
编辑xsl 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="sep" select="','"/>
<xsl:param name="test" select="','"/>
<xsl:param name="test2" select="','"/>
<xsl:template match="/">title,artist,country,company,price,year
<xsl:for-each select="catalog/cd">
<xsl:value-of select="concat('"', title, '"', $sep, '"', artist, '"', $sep, '"',
country, '"', $sep, '"', company, '"', $sep, price, $sep, year, ' ')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
解决方案
您可以使用xml
解析器来完成。像这样:
假设您的文件是test.xsl
. 然后你可以这样做:
import xml.etree.ElementTree as ET
tree = ET.parse('test.xsl')
root = tree.getroot()
match = [c.attrib for c in root if 'param' in c.tag]
然后match
看起来像这样:
>>> print(match)
[{'name': 'sep', 'select': "','"}, {'name': 'test', 'select': "','"}, {'name': 'test2', 'select': "','"}]
我假设您不需要整行,只需要<>
标签之间的属性。拥有这些属性将允许您创建所需的csv
文件。
推荐阅读
- ios - 如何将文件名传递给另一个 ViewController?
- agora.io - Agora.io : 使用 timeout 或 api 关闭未使用的频道
- flutter - 在颤动中使用更改通知器时监听属性
- reactjs - 使用反应导航添加后退按钮
- java - Firestore:仅当有更新时才从服务器获取数据,否则从缓存中获取
- python - 如何在 python 的 100x100 零矩阵中定期添加元素?
- jquery - .keyup 函数没有被触发
- fullcalendar - Fullcalendar - 是否可以在 resourceTimelineMonth 视图中设置日单元格内容 html?
- node.js - 使用自定义字符串读取 node.js/discord.js 中的 JSON 文件
- vue.js - Vuetify 数据表呈现嵌套数据