我有一个XML文件,我希望通过xslt 2.0文件对其进行处理,从而将其拆分为多个XML。我该怎么做?我目前正在使用Python 2.7.6。我尝试使用以下代码:
import lxml.etree as ET
dom = ET.parse(xml_filename)
xslt = ET.parse(xsl_filename)
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(ET.tostring(newdom, pretty_print=True))
但是这将返回None。
示例XML文件
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<name>dataset containing bounding box labels on images</name>
<comment>created by BBTag</comment>
<tags>
<tag name="Perimeter-SVT" color="#f9e99c"/>
<tag name="Perimeter-Vivon" color="#032585"/>
<tag name="ScoreBoard-Vivon" color="#bf5786"/>
<tag name="Perimeter-StarSports" color="#12dadd"/>
</tags>
<images>
<image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0011.jpg">
<box top="505" left="327" width="56" height="29">
<label>ScoreBoard-Vivon</label>
</box>
<box top="218" left="387" width="67" height="24">
<label>Perimeter-SVT</label>
</box>
</image>
<image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0005.jpg">
<box top="254" left="159" width="64" height="23">
<label>Perimeter-Vivon</label>
</box>
<box top="255" left="225" width="61" height="20">
<label>Perimeter-Vivon</label>
</box>
<box top="254" left="285" width="63" height="23">
<label>Perimeter-Vivon</label>
</box>
<box top="253" left="357" width="58" height="24">
<label>Perimeter-Vivon</label>
</box>
<box top="254" left="424" width="56" height="25">
<label>Perimeter-Vivon</label>
</box>
<box top="256" left="484" width="65" height="23">
<label>Perimeter-Vivon</label>
</box>
<box top="507" left="326" width="58" height="26">
<label>ScoreBoard-Vivon</label>
</box>
</image>
<image file="/var/www/html/tamsports.com/resources/videos/STAR_SPORTS_2_20170812/STAR_SPORTS_2_20170812-0009.jpg">
<box top="249" left="400" width="59" height="29">
<label>Perimeter-StarSports</label>
</box>
</image>
</images>
</dataset>
XSLT 2.0文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="//dataset/tags">
<xsl:for-each select="./tag">
<xsl:variable name="tagName" select="@name" />
<xsl:result-document method="xml" href="{$tagName}.xml">
<dataset>
<xsl:copy-of select="/dataset/name"/>
<xsl:copy-of select="/dataset/comment"/>
<tags>
<xsl:copy-of select="/dataset/tags/tag[./@name = $tagName]"/>
</tags>
<images>
<xsl:for-each select="/dataset/images/image[./box/label/text() = $tagName]">
<image>
<xsl:copy-of select="./@file"/>
<xsl:copy-of select="./box[./label[./text() = $tagName]]"/>
</image>
</xsl:for-each>
</images>
</dataset>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Python使用支持EXSLTexsl:document
http://exslt.org/exsl/index.html的libxslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:template match="dataset/tags">
<xsl:for-each select="tag">
<xsl:variable name="tagName" select="@name" />
<exsl:document method="xml" href="{$tagName}.xml">
<dataset>
<xsl:copy-of select="/dataset/name"/>
<xsl:copy-of select="/dataset/comment"/>
<tags>
<xsl:copy-of select="/dataset/tags/tag[@name = $tagName]"/>
</tags>
<images>
<xsl:for-each select="/dataset/images/image[box/label = $tagName]">
<image>
<xsl:copy-of select="@file"/>
<xsl:copy-of select="box[label = $tagName]"/>
</image>
</xsl:for-each>
</images>
</dataset>
</exsl:document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>