Actually I have a raw data which will have the information about scan rejected files. Each rejected item will log info. in 3 elements in raw data i.e i)ScanRejected ii) ScanInitiated iii) CardSupplier. I'm trying to process this raw data and put it in XML in a formatted way. While processing each record every time if I find <ScanRejected> element then I want to access <CardSupplier> element to get the CardNumber. it means I need to access ONLY immediate previous CardSupplier element.

Here in this case I need to get card number as
<CardNumber t="String">##############0251</CardNumber> NOT <CardNumber t="String">##############1111</CardNumber>

  • INPUT:

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

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

Dani AI

Generated

The sample output you posted pairs each ScanRejected with the CardSupplier that appears after it in the stream. That means you should select the next CardSupplier in document order, not a “previous” one — if your intention differs, swap the axis described below. The positional approach mentioned by (indexing ScanRejected[1] → CardSupplier[1], etc.) works only when the two sequences are guaranteed 1:1 and in the same order; it becomes fragile if a CardSupplier is missing or the event ordering changes.

A simple, robust XSLT 1.0 pattern (match each ScanRejected and take the immediately following CardSupplier) looks like this:

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

If the logical relationship is actually to the previous CardSupplier, use preceding-sibling::CardSupplier[1] instead. For documents where CardSupplier is not a direct sibling or where a reliable transaction identifier exists (for example a TransactionReference), use an xsl:key on CardSupplier and look it up by that ID — that is much more stable for real data.

Troubleshooting tips: test edge cases (missing CardSupplier, extra interleaved nodes), confirm all elements are in the same namespace (adjust XPath for namespaces), and include a sensible default when no CardNumber is found. Mentioned approaches keep intent explicit and avoid the brittle positional coupling that described.

use xslt

xsl file

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

            <xsl:apply-templates select="root/ScanRejected"/>
        </root>
    </xsl:template>
    <xsl:template match="ScanRejected">
        <xsl:variable select="position()" name="pos"/>
        <Tran>
            <!--
            <pos>

                <xsl:value-of select="$pos"/>
            </pos>
            -->
            <TranName>
                <xsl:value-of select="EventID"/>
            </TranName>

            <CardNumber>
                <xsl:value-of select="../CardSupplier[$pos]/CardNumber"/>
            </CardNumber>
        </Tran>
    </xsl:template>
</xsl:stylesheet>

your xml fil must be xml rules
i insert root tag

<?xml version="1.0"?>
<root>
    <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>
</root>

output

<?xml version='1.0' ?>
<root>
  <Tran>
    <TranName>ScanRejected</TranName>
    <CardNumber>##############0251</CardNumber>
  </Tran>
  <Tran>
    <TranName>ScanRejected</TranName>
    <CardNumber>##############1111</CardNumber>
  </Tran>
  <Tran>
    <TranName>ScanRejected</TranName>
    <CardNumber>##############2345</CardNumber>
  </Tran>
</root>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.