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



我是XSLT的新手,正在尝试解决一些问题。你能帮我解决这个问题吗。

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

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

输入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>

设计输出

    -<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>

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

<!-- 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>

相关内容

  • 没有找到相关文章

最新更新