Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何将元素从字符串转换为数组_Xml_Xslt - Fatal编程技术网

Xml 如何将元素从字符串转换为数组

Xml 如何将元素从字符串转换为数组,xml,xslt,Xml,Xslt,下面是我目前正在使用的XML ` <ns2:FlightSummary Version="5.0.0" xmlns:ns="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance"> <ns2:FlightSummary ID="JXXK2019" PartitionID="FLS" SequenceNmbr="3" TimeStamp

下面是我目前正在使用的XML

`
<ns2:FlightSummary Version="5.0.0" xmlns:ns="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance">
    <ns2:FlightSummary ID="JXXK2019" PartitionID="FLS" SequenceNmbr="3" TimeStamp="2019-08-13T09:55:18.381" HasInvoices="false" UpdateTimeStamp="2019-08-13T09:55:18.070"/>
    <ns:GetReservationRS>
        <ns:Reservation>
            <ns:Reservation>
                <ns:Segments>
                    <ns:PoA>
                        <ns:Airport>NRT</ns:Airport>
                        <ns:Departure>2019-08-13T09:25:00</ns:Departure>
                    </ns:PoA>
                    <ns:Segment sequence="1" id="15">
                        <ns:Air sequence="1" DayOfWeekInd="2">
                            <ns:DepartureAirport>NRT</ns:DepartureAirport>
                            <ns:ArrivalAirport>TPE</ns:ArrivalAirport>
                            <ns:OperatingAirlineCode>JL</ns:OperatingAirlineCode>
                            <ns:OperatingAirlineShortName>JAPAN AIRLINES</ns:OperatingAirlineShortName>
                            <ns:OperatingFlightNumber>5045</ns:OperatingFlightNumber>
                            <ns:EquipmentType>ERD</ns:EquipmentType>
                            <ns:MarketingAirlineCode>JL</ns:MarketingAirlineCode>
                            <ns:MarketingFlightNumber>5045</ns:MarketingFlightNumber>
                            <ns:DepartureDateTime>2019-08-13T09:25:00</ns:DepartureDateTime>
                            <ns:ArrivalDateTime>2019-08-13T12:10:00</ns:ArrivalDateTime>
                            <ns:FlightNumber>5045</ns:FlightNumber>
                        </ns:Air>
                    </ns:Segment>
                </ns:Segments>
            </ns:Reservation>
        </ns:Reservation>
    </ns:GetReservationRS>
</ns2:FlightSummary>
`
`
NRT
2019-08-13T09:25:00
NRT
热塑性弹性体
JL
日本航空公司
5045
ERD
JL
5045
2019-08-13T09:25:00
2019-08-13T12:10:00
5045
`
有时,segment元素可能包含多个段,在这种情况下,传入的XML以数组的形式显示数据。有时,我可能只有一个值,数据显示为字符串而不是数组。要添加到这一点,有时XML消息可能不包含任何数据段,在这种情况下,数据显示为

如何将元素从字符串转换为数组? 我正在使用以下XSLT将字符串转换为数组,但是,我认为它不正确,因为它按原样显示数据

``
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:json="http://json.org/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns:ns="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance"
xmlns:array="http://www.w3.org/2005/xpath-functions/array" 
version="1.0">

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>


    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="name(..) = 'ns:Segment'">
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <xsl:attribute name="json:force-array">True</xsl:attribute>
                    <xsl:apply-templates select="node()"/>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="node() | @*"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>``
``
真的
``

如果您发布XML输入和所需输出,它将帮助您获得一些答案。如果您发布XML输入和所需输出,它将帮助您获得一些答案。