Skip to main content

How do i convert a CSV file to a XML?

Hi @giarcnomis, firstly you will have to clarify the hierarchy structure of destination XML document, relationship between the XML structure and schema of the CSV table, and find key attributes to group descendant elements, in order to configure template expressions properly.

I assume as:

  • <Header> is the root element of destination XML document, and "FileID" is the key.
  • <Schedule> can occur one or more times under <Header>, and "CalendarGroupCode" is the key.
  • <Route> can occur one or more times under each <Schedule>, and "RoutePath" is the key.
  • <Trip> can occur one or more times under each <Route>, and "TripNumber" is the key.
  • <Stops> can occur one or more times under each <Trip>.

If my assumption above was correct, this workflow creates desired XML.

ROOT Expression:

<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <FileID>{fme:get-attribute("FileID")}</FileID>
    <FileSet>{fme:get-attribute("FileSet")}</FileSet>
    <MasterStartDate>{fme:get-attribute("MasterStartDate")}</MasterStartDate>
    <ServiceProvider>{fme:get-attribute("ServiceProvider")}</ServiceProvider>
    <ServiceProviderID>{fme:get-attribute("ServiceProviderID")}</ServiceProviderID>
    <DepotName>{fme:get-attribute("DepotName")}</DepotName>
    <DepotID>{fme:get-attribute("DepotID")}</DepotID>
    {
        fme:process-features("SCHEDULE")
    }
</Header>

SCHEDULE Expression:

<Schedule>
    <CalendarGroupName>{fme:get-attribute("CalendarGroupName")}</CalendarGroupName>
    <CalendarGroupCode>{fme:get-attribute("CalendarGroupCode")}</CalendarGroupCode>
    {
        fme:process-features("ROUTE",
            ("CalendarGroupCode"),
            (fme:get-attribute("CalendarGroupCode"))
        )
    }
</Schedule>

ROUTE Expression:

<Route>
    <RoutePath>{fme:get-attribute("RoutePath")}</RoutePath>
    <RouteNumber>{fme:get-attribute("RouteNumber")}</RouteNumber>
    <RouteDir>{fme:get-attribute("RouteDir")}</RouteDir>
    <PathNote>{fme:get-attribute("PathNote")}</PathNote>
    {
        fme:process-features("TRIP",
            ("CalendarGroupCode", "RoutePath"),
            (fme:get-attribute("CalendarGroupCode"), fme:get-attribute("RoutePath"))
        )
    }
</Route>

TRIP Expression:

<Trip>
    <TripNumber>{fme:get-attribute("TripNumber")}</TripNumber>
    <TicketCode>{fme:get-attribute("TicketCode")}</TicketCode>
    <IsPublic>{fme:get-attribute("IsPublic")}</IsPublic>
    <SchoolService>{fme:get-attribute("SchoolService")}</SchoolService>
    <WCA>{fme:get-attribute("WCA")}</WCA>
    <TripNote>
      <TripNoteText>{fme:get-attribute("TripNoteText")}</TripNoteText>
      <TripNoteCode>{fme:get-attribute("TripNoteCode")}</TripNoteCode>
    </TripNote>
    {
        fme:process-features("STOPS",
            ("CalendarGroupCode", "RoutePath", "TripNumber"),
            (fme:get-attribute("CalendarGroupCode"), fme:get-attribute("RoutePath"), fme:get-attribute("TripNumber"))
        )
    }
</Trip>

STOPS Expression:

<Stops>
    <StopID>{fme:get-attribute("StopID")}</StopID>
    <StopSequenceNumber>{fme:get-attribute("StopSequenceNumber")}</StopSequenceNumber>
    <IsTimingPoint>{fme:get-attribute("IsTimingPoint")}</IsTimingPoint>
    <ArrivalTime>{fme:get-attribute("ArrivalTime")}</ArrivalTime>
    <DepartureTime>{fme:get-attribute("DepartureTime")}</DepartureTime>
    <PickUpOnly>{fme:get-attribute("PickUpOnly")}</PickUpOnly>
    <DropOffOnly>{fme:get-attribute("DropOffOnly")}</DropOffOnly>
    <StopNote>
        <StopNoteText>{fme:get-attribute("StopNoteText")}</StopNoteText>
        <StopNoteCode>{fme:get-attribute("StopNoteCode")}</StopNoteCode>
    </StopNote>
</Stops>

0684Q00000ArJnbQAF.png

@takashi I'm having a similar issue, well along the same lines and wondered if you could help? You seem really experienced in this area of XML/HTTP. 

I have posted a question here:

https://knowledge.safe.com/questions/104778/http-caller-wont-work-with-xml-templater.html 

I'd really appreciate any help, I'm struggling with this a lot!

 

Thank you


Reply