XSLT:如何根据条件获取第一条记录

时间:2015-03-20 06:38:14

标签: xslt-1.0

我是这个XSLT的新手并试图解决一些问题。请你帮我解决这个问题。

实际上我有一个原始数据,其中包含有关扫描被拒绝文件的信息。每个被拒绝的项目都将记录信息原始数据中的3个元素,即i)ScanRejected ii)ScanInitiated iii)CardSupplier。 我正在尝试处理这些原始数据并以格式化的方式将其放入XML中。如果我找到<ScanRejected>元素,则每次处理每条记录时,我想访问<CardSupplier>元素以获取CardNumber。这意味着我需要访问以前的CardSupplier元素。

在这种情况下,我需要将卡号作为
<CardNumber t="String">##############0251</CardNumber> 不会 <CardNumber t="String">##############1111</CardNumber>

INPUT XML

    <ScanRejected>
      <Validity t="String">NotValidated</Validity>
      <EventID t="String">ScanRejected</EventID>
    </ScanRejected>
    <ScanInitiated>
      <EventID t="String">ScanInitiated</EventID>
      <TransactionReference t="String"></TransactionReference>
    </ScanInitiated>
    <CardSupplier>
      <CardNumber t="String">##############0251</CardNumber>
      <CardPosition t="String">Inserted</CardPosition>
      <CardValid t="Boolean">True</CardValid>
      <EventID t="String">CardAccepted</EventID>
    </CardSupplier>
    <ScanRejected>
      <Validity t="String">NotSupported</Validity>
      <EventID t="String">ScanRejected</EventID>
    </ScanRejected>
    <ScanInitiated>
      <EventID t="String">ScanInitiated</EventID>
      <TransactionReference t="String"></TransactionReference>
    </ScanInitiated>
    <CardSupplier>
      <CardNumber t="String">##############1111</CardNumber>
      <CardPosition t="String">Inserted</CardPosition>
      <CardValid t="Boolean">True</CardValid>
      <EventID t="String">CardAccepted</EventID>
    </CardSupplier>
    <ScanRejected>
      <Validity t="String">NotScanned</Validity>
      <EventID t="String">ScanRejected</EventID>
    </ScanRejected>
    <ScanInitiated>
      <EventID t="String">ScanInitiated</EventID>
      <TransactionReference t="String"></TransactionReference>
    </ScanInitiated>
    <CardSupplier>
      <CardNumber t="String">##############2345</CardNumber>
      <CardPosition t="String">Inserted</CardPosition>
      <CardValid t="Boolean">True</CardValid>
      <EventID t="String">CardAccepted</EventID>
    </CardSupplier>

Desidered OUTPUT

    -<Tran>
      <TranName>ScanRejected</TranName>
      <CardNumber>##############0251</CardNumber>
    </Tran>
    -<Tran>
      <TranName>ScanRejected</TranName>
      <CardNumber>##############1111</CardNumber>
    </Tran>
    -<Tran>
      <TranName>ScanRejected</TranName>
      <CardNumber>##############2345</CardNumber>
    </Tran>

**我的XSLT

<xsl:template name="ScanTemplate">
  <xsl:variable name="x" select ="Content/*[1]" />
  <Tran>
    <xsl:choose>
     <xsl:when test="$x/EventID='ScanRejected'">
        <TranName>ScanRejected</TranName>
      </xsl:when>
    </xsl:choose>
    <CardNumber> </CardNumber>
  </Tran>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

我不知道你到目前为止尝试了什么,但是这样的事情应该可以正常工作,至少能给你一个做你想做的事情的线索:

<!-- assuming the root element of the XML is "root" -->
<xsl:template match="root">
   <out>
    <xsl:apply-templates select="ScanRejected"/>
   </out>
</xsl:template>

<xsl:template match="ScanRejected">
    <Tran>
        <TranName>ScanRejected</TranName>
        <CardNumber><xsl:value-of select="preceding-sibling::CardSupplier[1]/CardNumber" /></CardNumber>
    </Tran>
</xsl:template>