将 XML 文本分成三个单独的块



我有一个要求,我需要将以下文本拆分为三个单独的XML块 A 需要是一个块,B 需要是一个块,C 需要另一个块。

<line cnt="16"><![CDATA[                         A               B         C]]></line>
<line cnt="17"/>
<line cnt="18"><![CDATA[TOTAL               2583000            0      4800000]]></line>
<line cnt="19"/>
<line cnt="20"><![CDATA[06:00 - 07:00        ValueA            ValueB   ValueC]]></line>
<line cnt="21"><![CDATA[07:00 - 08:00        ValueA            ValueB   ValueC]]></line>
<line cnt="22"><![CDATA[08:00 - 09:00        107625            0       200000]]></line>
<line cnt="23"><![CDATA[09:00 - 10:00        107625            0       200000]]></line>
<line cnt="24"><![CDATA[10:00 - 11:00        107625            0       200000]]></line>
<line cnt="25"><![CDATA[11:00 - 12:00        107625            0       200000]]></line>
<line cnt="26"><![CDATA[12:00 - 13:00        107625            0       200000]]></line>
<line cnt="27"><![CDATA[13:00 - 14:00        107625            0       200000]]></line>
<line cnt="28"><![CDATA[14:00 - 15:00        107625            0       200000]]></line>
<line cnt="29"><![CDATA[15:00 - 16:00        107625            0       200000]]></line>
<line cnt="30"><![CDATA[16:00 - 17:00        107625            0       200000]]></line>
<line cnt="31"><![CDATA[17:00 - 18:00        107625            0       200000]]></line>
<line cnt="32"><![CDATA[18:00 - 19:00        107625            0       200000]]></line>
<line cnt="33"><![CDATA[19:00 - 20:00        107625            0       200000]]></line>
<line cnt="34"><![CDATA[20:00 - 21:00        107625            0       200000]]></line>
<line cnt="35"><![CDATA[21:00 - 22:00        107625            0       200000]]></line>
<line cnt="36"><![CDATA[22:00 - 23:00        107625            0       200000]]></line>
<line cnt="37"><![CDATA[23:00 - 00:00        107625            0       200000]]></line>
<line cnt="38"><![CDATA[00:00 - 01:00        107625            0       200000]]></line>
<line cnt="39"><![CDATA[01:00 - 02:00        107625            0       200000]]></line>
<line cnt="40"><![CDATA[02:00 - 03:00        107625            0       200000]]></line>
<line cnt="41"><![CDATA[03:00 - 04:00        107625            0       200000]]></line>
<line cnt="42"><![CDATA[04:00 - 05:00        107625            0       200000]]></line>
<line cnt="43"><![CDATA[05:00 - 06:00        107625            0       200000]]></line>

传出块的示例需要

<Block>
<Contract ContractID="xxx"/>
<Location CodeAgency="xxx" LocID="A" LocQualifier="xxx"/>
<DataSeries ValueType="xxx" ValueQualifier="xxx" ValueUnit="xxx" ValueInterval="xxx">
<Data Value="ValueA" TimeFrom="2019-10-11T04:00:00Z" TimeTo="2019-10-11T05:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T05:00:00Z" TimeTo="2019-10-11T06:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T06:00:00Z" TimeTo="2019-10-11T07:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T07:00:00Z" TimeTo="2019-10-11T08:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T08:00:00Z" TimeTo="2019-10-11T09:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T09:00:00Z" TimeTo="2019-10-11T10:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T10:00:00Z" TimeTo="2019-10-11T11:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T11:00:00Z" TimeTo="2019-10-11T12:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T12:00:00Z" TimeTo="2019-10-11T13:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T13:00:00Z" TimeTo="2019-10-11T14:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T14:00:00Z" TimeTo="2019-10-11T15:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T15:00:00Z" TimeTo="2019-10-11T16:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T16:00:00Z" TimeTo="2019-10-11T17:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T17:00:00Z" TimeTo="2019-10-11T18:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T18:00:00Z" TimeTo="2019-10-11T19:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T19:00:00Z" TimeTo="2019-10-11T20:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T20:00:00Z" TimeTo="2019-10-11T21:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T21:00:00Z" TimeTo="2019-10-11T22:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T22:00:00Z" TimeTo="2019-10-11T23:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T23:00:00Z" TimeTo="2019-10-12T00:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-12T00:00:00Z" TimeTo="2019-10-12T01:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-12T01:00:00Z" TimeTo="2019-10-12T02:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-12T02:00:00Z" TimeTo="2019-10-12T03:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-12T03:00:00Z" TimeTo="2019-10-12T04:00:00Z"/>
</DataSeries>
</Block>
<Block>
<Contract ContractID="xxx"/>
<Location CodeAgency="xxx" LocID="B" LocQualifier="xxx"/>
<DataSeries ValueType="xxx" ValueQualifier="xxx" ValueUnit="xxx" ValueInterval="xxx">
<Data Value="ValueB" TimeFrom="2019-10-11T04:00:00Z" TimeTo="2019-10-11T05:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T05:00:00Z" TimeTo="2019-10-11T06:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T06:00:00Z" TimeTo="2019-10-11T07:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T07:00:00Z" TimeTo="2019-10-11T08:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T08:00:00Z" TimeTo="2019-10-11T09:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T09:00:00Z" TimeTo="2019-10-11T10:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T10:00:00Z" TimeTo="2019-10-11T11:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T11:00:00Z" TimeTo="2019-10-11T12:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T12:00:00Z" TimeTo="2019-10-11T13:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T13:00:00Z" TimeTo="2019-10-11T14:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T14:00:00Z" TimeTo="2019-10-11T15:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T15:00:00Z" TimeTo="2019-10-11T16:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T16:00:00Z" TimeTo="2019-10-11T17:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T17:00:00Z" TimeTo="2019-10-11T18:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T18:00:00Z" TimeTo="2019-10-11T19:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T19:00:00Z" TimeTo="2019-10-11T20:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T20:00:00Z" TimeTo="2019-10-11T21:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T21:00:00Z" TimeTo="2019-10-11T22:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T22:00:00Z" TimeTo="2019-10-11T23:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-11T23:00:00Z" TimeTo="2019-10-12T00:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-12T00:00:00Z" TimeTo="2019-10-12T01:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-12T01:00:00Z" TimeTo="2019-10-12T02:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-12T02:00:00Z" TimeTo="2019-10-12T03:00:00Z"/>
<Data Value="ValueB" TimeFrom="2019-10-12T03:00:00Z" TimeTo="2019-10-12T04:00:00Z"/>
</DataSeries>
</Block>
<Block>
<Contract ContractID="xxx"/>
<Location CodeAgency="xxx" LocID="C" LocQualifier="xxx"/>
<DataSeries ValueType="xxx" ValueQualifier="xxx" ValueUnit="xxx" ValueInterval="xxx">
<Data Value="ValueC" TimeFrom="2019-10-11T06:00:00Z" TimeTo="2019-10-11T07:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T07:00:00Z" TimeTo="2019-10-11T08:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T08:00:00Z" TimeTo="2019-10-11T09:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T09:00:00Z" TimeTo="2019-10-11T10:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T10:00:00Z" TimeTo="2019-10-11T11:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T11:00:00Z" TimeTo="2019-10-11T12:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T12:00:00Z" TimeTo="2019-10-11T13:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T13:00:00Z" TimeTo="2019-10-11T14:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T14:00:00Z" TimeTo="2019-10-11T15:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T15:00:00Z" TimeTo="2019-10-11T16:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T16:00:00Z" TimeTo="2019-10-11T17:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T17:00:00Z" TimeTo="2019-10-11T18:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T18:00:00Z" TimeTo="2019-10-11T19:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T19:00:00Z" TimeTo="2019-10-11T20:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T20:00:00Z" TimeTo="2019-10-11T21:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T21:00:00Z" TimeTo="2019-10-11T22:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T22:00:00Z" TimeTo="2019-10-11T23:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T23:00:00Z" TimeTo="2019-10-11T00:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T00:00:00Z" TimeTo="2019-10-11T01:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T01:00:00Z" TimeTo="2019-10-11T02:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T02:00:00Z" TimeTo="2019-10-11T03:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T03:00:00Z" TimeTo="2019-10-11T04:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T04:00:00Z" TimeTo="2019-10-11T05:00:00Z"/>
<Data Value="ValueC" TimeFrom="2019-10-11T05:00:00Z" TimeTo="2019-10-11T06:00:00Z"/>
</Block>

我正在尝试调整以下 XSLT 编码,这是一个可以根据要求显示的更多代码段。对我来说,主要困难是创建将位置 ID 及其相应的小时值分隔到块中的代码。

<xsl:template match="/">
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="from">
<xsl:value-of select="normalize-space(substring-before(substring-after(//line[contains(translate(text(), $uppercase, $smallcase),'period:')]/text(),':'),'-'))"/>
</xsl:variable>
<xsl:variable name="to">
<xsl:value-of select="normalize-space(substring-after(substring-after(//line[contains(translate(text(), $uppercase, $smallcase),'period:')]/text(),':'),'-'))"/>
</xsl:variable>
<xsl:variable name="auction">
<xsl:choose>
<xsl:when test="//line[contains(text(),'successful participation in the following auction')]">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>         
</xsl:variable>
<OwnerId>
<xsl:variable name="lineId">
<xsl:value-of select="//line[contains(translate(text(),$uppercase, $smallcase), 'the bid was placed by:') or contains(translate(text(),$uppercase, $smallcase), 'the capacity was booked by:')]/@cnt + 1"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains(//line[@cnt=$lineId]/text(),'Shell Energy')">
<xsl:value-of select ="2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select ="1"/>
</xsl:otherwise>
</xsl:choose>
</OwnerId>
<DealId>
<xsl:value-of select="normalize-space(substring-after(translate(//line[contains(translate(text(), $uppercase, $smallcase),'deal id:')]/text(),'/',' '),':'))"/>
</DealId>
<xsl:variable name="NB_Booking">
<xsl:value-of select="normalize-space(substring-after(//line[contains(translate(text(), $uppercase, $smallcase),'tso:')]/text(),':'))"/>
</xsl:variable>
<NetpointId>
<xsl:value-of select="concat(substring-before($NB_Booking,' '),'#',normalize-space(substring-after(//line[contains(translate(text(), $uppercase, $smallcase),'id of network point:')]/text(),':')))"/>
</NetpointId>
<Direction>
<xsl:value-of select="translate(normalize-space(substring-after(//line[contains(translate(text(), $uppercase, $smallcase),'direction:')]/text(),':')),'e','E')"/>
</Direction>
<DayAhead>
<!-- wenn milliseconds fuer addhour(from) = milliseconds fuer to dann true-->
<xsl:choose>
<xsl:when test="ut:getTimeStamp(ut:addDay($from,1,1,1),4) = ut:getTimeStamp($to,1)">
<xsl:value-of select="'true'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'false'"/>
</xsl:otherwise>
</xsl:choose>
</DayAhead>

请考虑以下简化示例:

.XML

<root>
<line cnt="20"><![CDATA[06:00 - 07:00        ValueA            ValueB   ValueC]]></line>
<line cnt="21"><![CDATA[07:00 - 08:00        ValueA            ValueB   ValueC]]></line>
<line cnt="22"><![CDATA[08:00 - 09:00        107625            0       200000]]></line>
<line cnt="23"><![CDATA[09:00 - 10:00        107625            0       200000]]></line>
<line cnt="24"><![CDATA[10:00 - 11:00        107625            0       200000]]></line>
<line cnt="25"><![CDATA[11:00 - 12:00        107625            0       200000]]></line>
<line cnt="26"><![CDATA[12:00 - 13:00        107625            0       200000]]></line>
<line cnt="27"><![CDATA[13:00 - 14:00        107625            0       200000]]></line>
<line cnt="28"><![CDATA[14:00 - 15:00        107625            0       200000]]></line>
<line cnt="29"><![CDATA[15:00 - 16:00        107625            0       200000]]></line>
<line cnt="30"><![CDATA[16:00 - 17:00        107625            0       200000]]></line>
<line cnt="31"><![CDATA[17:00 - 18:00        107625            0       200000]]></line>
<line cnt="32"><![CDATA[18:00 - 19:00        107625            0       200000]]></line>
<line cnt="33"><![CDATA[19:00 - 20:00        107625            0       200000]]></line>
<line cnt="34"><![CDATA[20:00 - 21:00        107625            0       200000]]></line>
<line cnt="35"><![CDATA[21:00 - 22:00        107625            0       200000]]></line>
<line cnt="36"><![CDATA[22:00 - 23:00        107625            0       200000]]></line>
<line cnt="37"><![CDATA[23:00 - 00:00        107625            0       200000]]></line>
<line cnt="38"><![CDATA[00:00 - 01:00        107625            0       200000]]></line>
<line cnt="39"><![CDATA[01:00 - 02:00        107625            0       200000]]></line>
<line cnt="40"><![CDATA[02:00 - 03:00        107625            0       200000]]></line>
<line cnt="41"><![CDATA[03:00 - 04:00        107625            0       200000]]></line>
<line cnt="42"><![CDATA[04:00 - 05:00        107625            0       200000]]></line>
<line cnt="43"><![CDATA[05:00 - 06:00        107625            0       200000]]></line>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="date">2019-10-11</xsl:param>
<xsl:template match="/root">
<result>
<xsl:for-each select="line">
<Data Value="{normalize-space(substring(., 22, 18))}">
<xsl:attribute name="TimeFrom">
<xsl:value-of select="$date"/>
<xsl:text>T</xsl:text>
<xsl:value-of select="substring(., 1, 5)"/>
<xsl:text>:00Z</xsl:text>
</xsl:attribute>
<xsl:attribute name="TimeTo">
<xsl:value-of select="$date"/>
<xsl:text>T</xsl:text>
<xsl:value-of select="substring(., 9, 5)"/>
<xsl:text>:00Z</xsl:text>
</xsl:attribute>
</Data>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<result>
<Data Value="ValueA" TimeFrom="2019-10-11T06:00:00Z" TimeTo="2019-10-11T07:00:00Z"/>
<Data Value="ValueA" TimeFrom="2019-10-11T07:00:00Z" TimeTo="2019-10-11T08:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T08:00:00Z" TimeTo="2019-10-11T09:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T09:00:00Z" TimeTo="2019-10-11T10:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T10:00:00Z" TimeTo="2019-10-11T11:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T11:00:00Z" TimeTo="2019-10-11T12:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T12:00:00Z" TimeTo="2019-10-11T13:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T13:00:00Z" TimeTo="2019-10-11T14:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T14:00:00Z" TimeTo="2019-10-11T15:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T15:00:00Z" TimeTo="2019-10-11T16:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T16:00:00Z" TimeTo="2019-10-11T17:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T17:00:00Z" TimeTo="2019-10-11T18:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T18:00:00Z" TimeTo="2019-10-11T19:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T19:00:00Z" TimeTo="2019-10-11T20:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T20:00:00Z" TimeTo="2019-10-11T21:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T21:00:00Z" TimeTo="2019-10-11T22:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T22:00:00Z" TimeTo="2019-10-11T23:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T23:00:00Z" TimeTo="2019-10-11T00:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T00:00:00Z" TimeTo="2019-10-11T01:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T01:00:00Z" TimeTo="2019-10-11T02:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T02:00:00Z" TimeTo="2019-10-11T03:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T03:00:00Z" TimeTo="2019-10-11T04:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T04:00:00Z" TimeTo="2019-10-11T05:00:00Z"/>
<Data Value="107625" TimeFrom="2019-10-11T05:00:00Z" TimeTo="2019-10-11T06:00:00Z"/>
</result>

相关内容

  • 没有找到相关文章

最新更新