我得到包含电视节目的列表,一个巨大的XML文件。 我必须把它分开到包含仅一天,所有广播小文件。 我设法到,但有两个问题与XML头,并有多次节点之中。
该XML的结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<broadcasts>
<broadcast>
<id>4637445812</id>
<week>39</week>
<date>2009-09-22</date>
<time>21:45:00:00</time>
... (some more)
</broadcast>
... (long list of broadcast nodes)
</broadcasts>
我的XSL是这样的:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:redirect="http://xml.apache.org/xalan/redirect"
extension-element-prefixes="redirect"
version="1.0">
<!-- mark the CDATA escaped tags -->
<xsl:output method="xml" cdata-section-elements="title text"
indent="yes" omit-xml-declaration="no" />
<xsl:template match="broadcasts">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="broadcast">
<!-- Build filename PRG_YYYYMMDD.xml -->
<xsl:variable name="filename" select="concat(substring(date,1,4),substring(date,6,2))"/>
<xsl:variable name="filename" select="concat($filename,substring(date,9,2))" />
<xsl:variable name="filename" select="concat($filename,'.xml')" />
<redirect:write select="concat('PRG_',$filename)" append="true">
<schedule>
<broadcast program="TEST">
<!-- format timestamp in specific way -->
<xsl:variable name="tmstmp" select="concat(substring(date,9,2),'/')"/>
<xsl:variable name="tmstmp" select="concat($tmstmp,substring(date,6,2))"/>
<xsl:variable name="tmstmp" select="concat($tmstmp,'/')"/>
<xsl:variable name="tmstmp" select="concat($tmstmp,substring(date,1,4))"/>
<xsl:variable name="tmstmp" select="concat($tmstmp,' ')"/>
<xsl:variable name="tmstmp" select="concat($tmstmp,substring(time,1,5))"/>
<timestamp><xsl:value-of select="$tmstmp"/></timestamp>
<xsl:copy-of select="title"/>
<text><xsl:value-of select="subtitle"/></text>
<xsl:variable name="newVps" select="concat(substring(vps,1,2),substring(vps,4,2))"/>
<xsl:variable name="newVps" select="concat($newVps,substring(vps,7,2))"/>
<xsl:variable name="newVps" select="concat($newVps,substring(vps,10,2))"/>
<vps><xsl:value-of select="$newVps"/></vps>
<nextday>false</nextday>
</broadcast>
</schedule>
</redirect:write>
</xsl:template>
</xsl:stylesheet>
我的输出个XML是这样的:
PRG_20090512.xml:
<?xml version="1.0" encoding="UTF-8"?>
<schedule>
<broadcast program="TEST">
<timestamp>01/03/2010 06:00</timestamp>
<title><![CDATA[TELEKOLLEG Geschichte ]]></title>
<text><![CDATA[Giganten in Fernost]]></text>
<vps>06000000</vps>
<nextday>false</nextday>
</broadcast>
</schedule>
<?xml version="1.0" encoding="UTF-8"?> <!-- don't want this -->
<schedule> <!-- don't want this -->
<broadcast program="TEST">
<timestamp>01/03/2010 06:30</timestamp>
<title><![CDATA[Die chemische Bindung]]></title>
<text/>
<vps>06300000</vps>
<nextday>false</nextday>
</broadcast>
</schedule>
<?xml version="1.0" encoding="UTF-8"?>
...and so on
我可以把省略的XML声明=“是”输出声明,但我没有任何XML头。 我试图把在一个检查,如果该标签已经在产量,但未能选择输出节点...
这是我的尝试:
<xsl:choose>
<xsl:when test="count(schedule) = 0"> <!-- schedule needed -->
<schedule>
<broadcast>
...
<xsl:otherwise> <!-- no schedule needed -->
<broadcast>
...
感谢您的帮助,因为我不知道如何处理。 ;(雪人