首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Python编辑EPG XML

用Python编辑EPG XML
EN

Stack Overflow用户
提问于 2022-08-02 20:24:53
回答 1查看 68关注 0票数 0

我是Python新手,我希望修改一个XML文件来改变周围的一些事情。我可以提供一个例子,然后是我想要的输出。

原版..。

代码语言:javascript
运行
复制
<programme channel="I9.11363.zap2it.com" start="20220729080000 -0500" stop="20220729090000 -0500">
    <title lang="en">Live with Kelly and Ryan</title>
    <sub-title lang="en">Live's Ready or Not Week; Live's Foodfluencer Friday Faceoff</sub-title>
    <desc lang="en">Making an emergency evacuation kit; a chef provides a summertime recipe.</desc>
    <date>20220729</date>
    <category lang="en">Talk</category>
    <category lang="en">Series</category>
    <length units="minutes">60</length>
    <icon src="https://zap2it.tmsimg.com/assets/p14101643_b_v13_ah.jpg" />
    <url>https://tvlistings.zap2it.com//overview.html?programSeriesId=SH02684484&amp;tmsId=EP026844841372</url>
    <episode-num system="common">S06E232</episode-num>
    <episode-num system="dd_progid">EP02684484.1372</episode-num>
    <episode-num system="xmltv_ns">5.231.</episode-num>
    <audio>
        <stereo>stereo</stereo>
    </audio>
    <new />
    <subtitles type="teletext" />
    <rating>
        <value>TV-PG</value>
    </rating>
</programme>

期望输出..。将"New“标记移到标题中,移除<episode-num system="common">S06E232</episode-num>并将其放置到描述中。

代码语言:javascript
运行
复制
<programme channel="I9.11363.zap2it.com" start="20220729080000 -0500" stop="20220729090000 -0500">
    <title lang="en">Live with Kelly and Ryan New</title>
    <sub-title lang="en">Live's Ready or Not Week; Live's Foodfluencer Friday Faceoff</sub-title>
    <desc lang="en">S06E232 (return)Making an emergency evacuation kit; a chef provides a summertime recipe. TV-PG 20220729 </desc>
    <icon src="https://zap2it.tmsimg.com/assets/p14101643_b_v13_ah.jpg" />
    <url>https://tvlistings.zap2it.com//overview.html?programSeriesId=SH02684484&amp;tmsId=EP026844841372</url>
</programme>
EN

回答 1

Stack Overflow用户

发布于 2022-08-02 21:02:33

下面是一个基于XSLT的解决方案。

输入XML

代码语言:javascript
运行
复制
<?xml version="1.0"?>
<programme channel="I9.11363.zap2it.com" start="20220729080000 -0500" stop="20220729090000 -0500">
    <title lang="en">Live with Kelly and Ryan</title>
    <sub-title lang="en">Live's Ready or Not Week; Live's Foodfluencer Friday Faceoff</sub-title>
    <desc lang="en">Making an emergency evacuation kit; a chef provides a summertime recipe.</desc>
    <date>20220729</date>
    <category lang="en">Talk</category>
    <category lang="en">Series</category>
    <length units="minutes">60</length>
    <icon src="https://zap2it.tmsimg.com/assets/p14101643_b_v13_ah.jpg"/>
    <url>https://tvlistings.zap2it.com//overview.html?programSeriesId=SH02684484&amp;tmsId=EP026844841372</url>
    <episode-num system="common">S06E232</episode-num>
    <episode-num system="dd_progid">EP02684484.1372</episode-num>
    <episode-num system="xmltv_ns">5.231.</episode-num>
    <audio>
        <stereo>stereo</stereo>
    </audio>
    <new/>
    <subtitles type="teletext"/>
    <rating>
        <value>TV-PG</value>
    </rating>
</programme>

XSLT

代码语言:javascript
运行
复制
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="title">
        <xsl:copy>
            <xsl:attribute name="lang">en</xsl:attribute>
            <xsl:value-of select="concat(., ' new')"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="desc">
        <xsl:copy>
            <xsl:attribute name="lang">en</xsl:attribute>
            <xsl:value-of select="concat(/programme/episode-num[@system='common'], ' ', .)"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="date | category | length | episode-num | audio | new | subtitles | rating"/>
</xsl:stylesheet>

输出XML

代码语言:javascript
运行
复制
<programme stop="20220729090000 -0500" channel="I9.11363.zap2it.com" start="20220729080000 -0500">
  <title lang="en">Live with Kelly and Ryan new</title>
  <sub-title lang="en">Live's Ready or Not Week; Live's Foodfluencer Friday Faceoff</sub-title>
  <desc lang="en">S06E232 Making an emergency evacuation kit; a chef provides a summertime recipe.</desc>
  <icon src="https://zap2it.tmsimg.com/assets/p14101643_b_v13_ah.jpg"/>
  <url>https://tvlistings.zap2it.com//overview.html?programSeriesId=SH02684484&amp;tmsId=EP026844841372</url>
</programme>

Python

代码语言:javascript
运行
复制
import os
import lxml.etree as ET

inputfile = "D:\\temp\\input.xml"
xsltfile = "D:\\temp\\process.xslt"
outfile = "D:\\output\\output.xml"



dom = ET.parse(inputfile)
xslt = ET.parse(xsltfile)
transform = ET.XSLT(xslt)
newdom = transform(dom,
              id=XSLT.strparam("bk101"),
              author=XSLT.strparam("New Author"))
infile = unicode((ET.tostring(newdom, pretty_print=True)))
outfile = open(outfile, 'a')
outfile.write(infile)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73213395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档