0

私はかなり複雑な XML 構造を持っていますが、小さなセクションに、次の構造のイン/アウト時間のリストと、そこに含まれる可能性のあるデータがあります。

<EventSummary>
   <Name>In/Out times</Name>
   <Entries>
      <Entry>
         <Name>In</Name>
         <Time>4/1/2013 10:45</Time>
         <Value>1</Value>
      </Entry>
      <Entry>
         <Name>In</Name>
         <Time>4/1/2013 10:55</Time>
         <Value>1</Value>
      </Entry>
      <Entry>
         <Name>Out</Name>
         <Time>4/1/2013 11:30</Time>
         <Value>0</Value>
      </Entry>
      <Entry>
         <Name>In</Name>
         <Time>4/1/2013 11:35</Time>
         <Value>1</Value>
      </Entry>
   </Entries>
</EventSummary>

したがって、エントリは時系列順であることが保証されますが、イン タイムと次のアウト タイムを調整する必要があります。したがって、上記の例では、イン タイムの後に別のイン ティムが続き、アウト タイムの後にイン タイムが続くことに注意してください。最終製品を次のようにする必要があります。この場合は次のようになります。

<Table>
  <Row>
    <Cell>
      <Text>4/1/2013 10:45</Text>
    </Cell>
    <Cell>
      <Text>4/1/2013 11:30</Text>
    </Cell>
  </Row>
  <Row>
    <Cell>
      <Text>4/1/2013 11:35</Text>
    </Cell>
  </Row>
</Table>

基本的に、イン/アウトのペアごとに行が必要です。したがって、最初の In を見つけて、最初の Out まで次の In をすべてスキップし、Out の後に別の In が見つかった場合は、新しい行を開始する必要があります。

エントリをループしているときに、インまたはアウトの検索から切り替える方法がわかりません。何か案は?

4

1 に答える 1

1

これはそれを行う必要があります:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <Table>
      <xsl:apply-templates
        select="EventSummary/Entries
                   /Entry[Name = 'In' and 
                          (not(preceding-sibling::Entry) or
                            preceding-sibling::Entry[1]/Name = 'Out')]" />
    </Table>
  </xsl:template>

  <xsl:template match="Entry[Name = 'In']">
    <Row>
      <xsl:apply-templates
        select="Time |
                following-sibling::Entry[Name = 'Out'][1]/Time" />
    </Row>
  </xsl:template>

  <xsl:template match="Time">
    <Cell>
      <Text>
        <xsl:value-of select="." />
      </Text>
    </Cell>
  </xsl:template>
</xsl:stylesheet>

サンプル入力で実行すると、結果は次のようになります。

<Table>
  <Row>
    <Cell>
      <Text>4/1/2013 10:45</Text>
    </Cell>
    <Cell>
      <Text>4/1/2013 11:30</Text>
    </Cell>
  </Row>
  <Row>
    <Cell>
      <Text>4/1/2013 11:35</Text>
    </Cell>
  </Row>
</Table>
于 2013-04-04T16:22:03.100 に答える