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>

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.