如何将XML元素的内容作为字符串获取?

2024-06-26 03:50:27 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑这个XML示例:

<data>
    <items>
        <item name="item1">item1pre <bold>ok!</bold> item1post</item>
        <item name="item2">item2</item>
    </items>
</data>

我正在寻找一种方法来获得以下结果:

“项目1准备**好!**“项目1后”

我想把item1的所有内容作为字符串“item1pre<;bold>;好!<;'/bold>;item1post“,然后将“<;'bold>;”和“<;'/bold>;”替换为“*”,但我不知道如何得到它。你知道吗


Tags: 项目nameltgt示例dataitemsok
2条回答
xml="""
<data>
    <items>
        <item name="item1">item1pre<bold>ok!</bold>item1post</item>
        <item name="item2">item2</item>
    </items>
</data> 
"""

import xml.etree.ElementTree as ET
# python included module

def cleaned_strings_from_xml(xml_str, tag='item'):
    """
    finds all items of type tag from xml-string

    :param xml_str: valid xml structure as string
    :param tag: tag to search inside the xml
    :returns: list of all texts of 'tag'-items
    """
    strings = []
    root = ET.fromstring(xml)
    for item in root.iter(tag):
        item_str = ET.tostring(item).decode('utf-8')
        item_str = item_str.replace('<bold>', ' **').replace('</bold>', ' **')
        strings.append(ET.fromstring(item_str).text)
    return strings

print(cleaned_strings_from_xml(xml))

通过使用xslt转换,可以将所有xml处理卸载到libxml中。Libxml是用C编写的,速度应该更快:

from lxml import etree

transform = etree.XSLT(etree.XML('''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="data/items/item[@name = 'item1']">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="text()"/>
        <xsl:text>**</xsl:text>
        <xsl:value-of select="bold/."/>
        <xsl:text>**</xsl:text>
        <xsl:value-of select="bold/following-sibling::text()[1]"/>
        <xsl:text>"</xsl:text>
    </xsl:template>

    <xsl:template match="data/items/item[@name != 'item1']" />
</xsl:stylesheet>
'''))

with open("source.xml") as f:
    print(transform(etree.parse(f)))

简而言之:将item元素与name属性“item1”匹配,然后使用相对xpath表达式来提取字符串。你知道吗

相关问题 更多 >