如何通过转换输入XML在Ruby 1.8.7中插入XML元素

时间:2012-01-03 19:10:59

标签: ruby xml xslt

我正在使用Ruby 1.8.7并且需要通过插入一些值基于MealType和其他条件的标签将MealRQ输入XML转换为MealRS输出XML。有一种简单的方法可以使用某些XML库进行处理,还是最好用参数调用XSL模板?我是Ruby的新手,我在RHEL5 linux环境下工作。在某些情况下,我只是简单地调用系统xsltproc和其他输入xmls的模板,它不需要任何参数,但我想将代码完全带入Ruby。我听说过Nokogiri和其他一些像Hpricot,但没有设置它们的经验,如果他们可以使用1.8.7

<MealRQ>
<User>
    <Userid>abc123</Userid>
    <Location>Asia</Location>
<User>
<Requests>
    <Request>
        <Pax>
            <FirstName>FIRSTNM</FirstName>
            <LastName>LASTNM</LastName>
        </Pax>
        <Segments>
            <Segment>
                <PNR>YY123</PNR>
                <Airline>YY</Airline>
                <FlightNum>100</FlightNum>
                <Origin>JFK</Origin>
                <Destination>DFW</Destination>
                <DeptrDate>2011-12-25</DeptrDate>
                <DeptrTime>18:30</DeptrTime>
                <MealType>VEG</MealType>
                <MealRemark>LOW SPICE</MealRemark>
            </Segment>
            <Segment>
                <PNR>ZZ123</PNR>
                <Airline>ZZ</Airline>
                <FlightNum>100</FlightNum>
                <Origin>DFW</Origin>
                <Destination>JFK</Destination>
                <DeptrDate>2011-12-26</DeptrDate>
                <MealType>VEG</MealType>
                <MealRemark>LOW SPICE</MealRemark>
            </Segment>
        </Segments>
    </Request>
</Requests>

输出XML:

<MealRS>
<User>
    <Userid>abc123</Userid>
    <Location>Asia</Location>
<User>
<Requests>
    <Request>
        <Pax>
            <FirstName>FIRSTNM</FirstName>
            <LastName>LASTNM</LastName>
        </Pax>
        <Segments>
            <Segment>
                <PNR>YY123</PNR>
                <Airline>YY</Airline>
                <FlightNum>100</FlightNum>
                <Origin>JFK</Origin>
                <Destination>DFW</Destination>
                <DeptrDate>2011-12-25</DeptrDate>
                <MealType>AVML</MealType>
                <MealRemark>LOW SPICE</MealRemark>
                <!-- DeptrTime tag is dropped in response -->
                <!-- Five new tags inserted in each Segment -->
                <!-- Fail if Deptr Date/Time is less than 24 hr-->
                <!-- The next 3 tags will be empty if failed -->
                <Currency></Currency>
                <Amount></Amount>
                <TaxCode></TaxCode>
                <Status>Failed</Status>
                <StatusRemark>TOO LATE TO BOOK</StatusRemark>
            </Segment>
            <Segment>
                <PNR>ZZ123</PNR>
                <Airline>ZZ</Airline>
                <FlightNum>100</FlightNum>
                <Origin>DFW</Origin>
                <Destination>JFK</Destination>
                <DeptrDate>2011-12-26</DeptrDate>
                <MealType>VEG</MealType>
                <MealRemark>LOW SPICE</MealRemark>
                <!-- these success values are for VEG MEAL -->
                <Currency>USD</Currency>
                <Amount>15</Amount>
                <TaxCode>QQ</TaxCode>
                <Status>Success</Status>
                <StatusRemark>FIRSTNM ZZ100 CONF</StatusRemark>
            </Segment>
        </Segments>
    </Request>
</Requests>

0 个答案:

没有答案