排解 XML 到 JSON 政策執行階段錯誤的問題

您正在查看 Apigee Edge 說明文件。
前往 Apigee X說明文件
資訊

SourceUnavailable

錯誤代碼

steps.xml2json.SourceUnavailable

錯誤回應主體

{
    "fault": {
        "faultstring": "XMLToJSON[policy_name]: Source [source_variable] is not available",
        "detail": {
            "errorcode": "steps.xmltojson.SourceUnavailable"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "XMLToJSON[Convert-XMLToJSON]: Source response is not available",
        "detail": {
            "errorcode": "steps.xml2json.SourceUnavailable"
        }
    }
}

原因

如果 XML 到 JSON 政策的 <Source> 元素中指定的 message 或字串變數為下列任一情況,就會發生這個錯誤:

  • 超出範圍 (無法在執行政策的特定流程中使用),或
  • 無法解析 (未定義)

舉例來說,如果 XML 到 JSON 政策應在要求流程中執行,但 <Source> 元素設為 response 變數,而該變數不存在於要求流程中,就會發生這項錯誤。

診斷

  1. 找出發生錯誤的 XML 到 JSON 政策,以及無法使用的變數名稱。您可以在錯誤回應的 faultstring 元素中找到這兩項項目。舉例來說,下列 faultstring 中的政策名稱為 Convert-XMLToJSON,變數為 response

    "faultstring": "XMLToJSON[Convert-XMLToJSON]: Source response is not available"
    
  2. 在 XML 至 JSON 政策 XML 轉換失敗的情況下,請確認 <Source> 元素中設定的變數名稱與錯誤字串中指出的變數名稱相符 (請參閱上述步驟 1)。舉例來說,下列 XML 至 JSON 政策會在 <Source> 元素中指定名為 response 的變數,且這與 faultstring 中的變數相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <XMLToJSON async="false" continueOnError="false" enabled="true" name="Convert-XMLToJSON">
        <DisplayName>Convert-XMLToJSON</DisplayName>
        <Properties/>
        <Format>google</Format>
        <OutputVariable>response</OutputVariable>
        <Source>response</Source>
    </XMLToJSON>
    
  3. 判斷是否已定義 <Source> 元素中使用的變數,並在執行 XML 到 JSON 政策的流程中使用。

  4. 如果變數為以下任一變數:

    • 超出範圍 (不適用於執行政策的特定流程)
    • 無法解析 (未定義)

    則是錯誤的原因。

    舉例來說,假設上述 XML 至 JSON 政策應在 request 流程中執行。提醒您,response 變數在 XML 到 JSON 政策的 <Source> 元素中使用。回應變數僅適用於回應流程。

    由於回應變數不在要求流程中,因此您會收到以下錯誤代碼:

    steps.xml2json.SourceUnavailable
    

解析度

請確認在失敗的 XML 至 JSON 政策的 <Source> 元素中設定的變數已定義,且在政策執行的流程中存在。

如要將範例 XML 修正為上述的 JSON 政策,您可以修改 <Source> 元素,以使用要求流程中的 request 變數:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="Convert-XMLToJSON">
    <DisplayName>Convert-XMLToJSON</DisplayName>
    <Properties/>
    <Format>google</Format>
    <OutputVariable>response</OutputVariable>
    <Source>request</Source>
</XMLToJSON>

ExecutionFailed

錯誤代碼

steps.xml2json.ExecutionFailed

錯誤回應主體

{
    "fault": {
        "faultstring": "XMLToJSON[policy_name]: Execution failed. reason: Premature end of document while parsing at line [line_number](possibly  around char [character_number])",
        "detail": {
            "errorcode": "steps.xml2json.ExecutionFailed"
        }
    }
}

可能原因

導致這項錯誤的可能原因包括:

原因 說明
缺少輸入酬載 輸入酬載 (XML) 為空白。
無效或格式不正確的輸入內容 傳送至 XML 到 JSON 政策的輸入內容 (XML) 無效或格式錯誤。

原因:缺少輸入酬載

在 XML 到 JSON 政策中,如果 <Source> 元素中指定的變數內容 (酬載) 為空白,就會發生此錯誤。

舉例來說,如果 XML 到 JSON 政策中的 <Source> 元素設為 requestresponse 變數,且應包含 XML 酬載,則如果酬載為空白,就會發生這個錯誤。

診斷

  1. 找出發生錯誤的 XML 到 JSON 政策。您可以在錯誤回應的 faultstring 元素中找到這項資訊。例如,在以下 faultstring 中,政策名稱為 Convert-XMLToJSON

    "faultstring": "XMLToJSON[Convert-XMLToJSON]: Execution failed. reason: Premature end of document while parsing at line 1(possibly  around char 0)"
    
  2. 請檢查失敗的 XML 到 JSON 政策 XML 中的 <Source> 元素,並判斷指定的變數。例如,下列 XML 到 JSON 政策將 <Source> 元素設為要求:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <XMLToJSON async="false" continueOnError="false" enabled="true" name="Convert-XMLToJSON">
        <DisplayName>Convert-XMLToJSON</DisplayName>
        <Properties/>
        <Options>
            <RecognizeNumber>true</RecognizeNumber>
            <RecognizeBoolean>true</RecognizeBoolean>
            <RecognizeNull>true</RecognizeNull>
        </Options>
        <OutputVariable>request</OutputVariable>
        <Source>request</Source>
    </XMLToJSON>
    
  3. 檢查 XMLToJSON 政策中針對 <Source> 元素指定的變數是否為空白。如果是空白,則表示這是錯誤的原因。

    在上方顯示的 XML 到 JSON 政策範例中,用戶端所傳送的要求酬載 (即要求主體) 為空白。

    例如:

    curl -v "http://<org>-<env>.apigee.net/v1/testxmltojson" -H "Content-Type: application/xml"
    

    由於 XML 要求酬載為空白,您會收到以下錯誤代碼:

    steps.xml2json.ExecutionFailed
    

    如果 <Source> 元素設為回應,但後端伺服器傳遞空白酬載,也會發生這項錯誤。

解析度

確認透過 <Source> 元素傳遞至 XML 政策的輸入內容是有效的 XML 酬載,並非空白。

如要修正 XML 到 JSON 政策範例的問題,請傳遞有效的 XML 酬載。例如:

  1. 建立名為 city.xml 的檔案,並在當中加入下列內容:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <City>Bengaluru</City>
       <Name>Apigee</Name>
       <Pincode>560016</Pincode>
    </root>
    
  2. 使用 cURL 指令發出 API 呼叫,如下所示:

    curl -v "http://<org>-<env>.apigee.net/v1/testxmltojson" -H "Content-Type: application/xml" -X POST -d @company.xml
    

原因:輸入無效或格式錯誤

如果 XML 到 JSON 政策剖析無效或格式錯誤的輸入內容,就會發生這個錯誤。

舉例來說,如果提供以下無效的 XML 做為 XML 至 JSON 政策的輸入內容,

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <City>Bengaluru</City>
   <Name>Apigee</Name>
   <Pincode>560016</Pincode>

您會收到以下錯誤訊息:

"faultstring": "XMLToJSON[Convert-XMLToJSON]: Execution failed. reason: Premature end of document while parsing at line 6(possibly  around char 0)"

診斷

  1. 找出發生錯誤的 XML 到 JSON 政策。您可以在錯誤回應的 faultstring 元素中找到這項資訊。例如,在以下 faultstring 中,政策名稱為 Convert-XMLToJSON

    "faultstring": "XMLToJSON[Convert-XMLToJSON]: Execution failed. reason: Premature end of document while parsing at line 6(possibly  around char 0)"
    
  2. 請檢查失敗的 XML 到 JSON 政策 XML 中指定的 <Source> 元素。舉例來說,下列 XML 對 JSON 政策的 <Source> 元素已設為 request 變數:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <XMLToJSON async="false" continueOnError="false" enabled="true" name="Convert-XMLToJSON">
        <DisplayName>Convert-XMLToJSON</DisplayName>
        <Properties/>
        <Options>
            <RecognizeNumber>true</RecognizeNumber>
            <RecognizeBoolean>true</RecognizeBoolean>
            <RecognizeNull>true</RecognizeNull>
        </Options>
        <OutputVariable>request</OutputVariable>
        <Source>request</Source>
    </XMLToJSON>
    
  3. 驗證 XML 至 JSON 政策 <Source> 元素中指定的輸入內容是否為有效的 XML 酬載。如果輸入值無效或格式錯誤,就會導致錯誤。

    在上述 XML 到 JSON 政策範例中,系統透過 city.xml 檔案將下列無效的 XML 傳遞至「提取變數」政策:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <City>Bengaluru</City>
       <Name>Apigee</Name>
       <Pincode>560016</Pincode>
    

    以下 API 呼叫範例說明要求的傳遞方式:

    curl -v "http://<org>-<env>.apigee.net/v1/testxmltpjson" -H "Content-Type: application/xml" -X POST -d @city.xml
    

    傳遞至 API 的 XML 酬載無效,因為 XML 沒有 <root> 元素的結束標記。您會收到錯誤代碼:

    steps.xml2json.ExecutionFailed
    

    如果 <Source> 元素設為回應,但後端伺服器的 XML 回應酬載無效或格式錯誤,也會發生這項錯誤。

解析度

請確認透過 <Source> 元素傳遞至 XML 至 JSON 政策的輸入內容有效且格式正確。

如要修正上述 XML 到 JSON 政策範例的問題,請按照下列方式傳遞有效的 XML 酬載要求:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <City>Bengaluru</City>
   <Name>Apigee</Name>
   <Pincode>560016</Pincode>
</root>

OutputVariableIsNotAvailable

錯誤代碼

steps.xml2json.OutputVariableIsNotAvailable

錯誤回應主體

{
    "fault": {
        "faultstring": &quot;XMLToJSON[policy_name]: Output variable is not available.",
        "detail": {
            "errorcode": "steps.xml2json.OutputVariableIsNotAvailable"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "XMLToJSON[Convert-XMLToJSON]: Output variable is not available.",
        "detail": {
            "errorcode": "steps.xml2json.OutputVariableIsNotAvailable"
        }
    }
}

原因

如果 XML 到 JSON 政策的 <Source> 元素中指定的變數是類型字串,且未定義 <OutputVariable> 元素,就會發生這個錯誤。如果 <Source> 元素中定義的變數屬於 string 類型,就必須指定 <OutputVariable> 元素。

診斷

  1. 找出發生錯誤的 XML 到 JSON 政策。您可以在錯誤回應的 faultstring 元素中找到這個值。舉例來說,下列 faultstring 中的政策名稱為 Convert-XMLToJSON

    "faultstring": "XMLToJSON[Convert-XMLToJSON]: Output variable is not available."
    
  2. 在 XML 至 JSON 政策驗證失敗的情況下,檢查是否缺少 <OutputVariable>

    以下是缺少 <OutputVariable> 元素的 XML 至 JSON 政策範例。

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    XMLToJSON async="false" continueOnError="f>alse&<quot; enabl>ed="true&quo<t; name=&quo>t;Con<vert-XMLToJ>SON&q<uot;
     >   Dis<playNam>eConv<ert-XM>LToJSON/Displa<yName
     > <  Properti>es/
        Formatgoogle/Format
        SourceTrackingNumber/Source
    /XMLToJSON
    
    
  3. 判斷 <Source> 元素中指定的變數類型:

    1. 在 API Proxy 組合中找出先定義變數的程式碼。
    2. 找出先定義及填入變數的政策後,您需要依下列方式判斷該變數的類型:
      1. 檢查 type 屬性值 (如有)。
      2. 如果沒有類型屬性,則系統會將變數視為字串。
    3. 如果變數類型為 string,就會造成錯誤。如要瞭解常見的變數及其類型,請參閱變數參考資料

    舉例來說,請查看上述 XML 到 JSON 政策中的 TrackingNumber 變數。其類型為字串。請考慮使用指派訊息政策,將值設為名為 TrackingNumber 的變數,如下所示:

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    AssignMessage async="false" continueOnError="false>"<; enabled=&>quot;true" name=<"Assign>_Trac<kingNumber&>quot;<
        DisplayNa>meAssign_<Trac>kingNumber/Dis<playN>ame
        P<roper><ties/
      <  As>signVariable
      <     > N><ameTra>ckingNumb<er/N>ame
     <       Value![C>DATA[<Code560075393539898/Code]>]/Va<lue
            Ref/
        /Assi>gnVar<iable
        IgnoreUnresolvedVariablestrue/IgnoreUnresolvedVar>i<ables
        Assi>gnTo createNew="false" transport="http" type="request"/
    /AssignMessage
    

    請注意,透過 <AssignVariable> 設定的變數類型為字串。因此變數 TrackingNumber 的型別為字串。

    現在,請回想一下,在 XML 到 JSON 政策的 <Source> 元素中使用 TrackingNumber 變數:

    <Source>TrackingNumber</Source>
    

    由於 TrackingNumber 屬於字串類型,且政策中缺少 <OutputVariable>,因此您會收到以下錯誤代碼:

    steps.xml2json.OutputVariableIsNotAvailable
    

解析度

請確認如果 XMLToJSON 政策的 <Source> 元素中指定的變數為字串類型,則在這種情況下必須使用 <OutputVariable> 元素。

如要修正上述的 XML 至 JSON 政策,請加入 <OutputVariable> 元素,如下所示。

<?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
XMLToJSON async="false" continueOnError="f>alse&<quot; enabl>ed="true&quo<t; name=&quo>t;Con<vert-XMLToJ>SON&q<uot;
 >   Dis<playNam>eConv<ert-XMLToJSON/>DisplayN<ame
    Propert>ies/
<    Fo>rmatgoogle/For<mat
   > <OutputVari>ableresponse/OutputVariable
    SourceTrackingNumber/Source
/XMLToJSON

InCompatibleTypes

錯誤代碼

steps.xml2json.InCompatibleTypes

錯誤回應主體

{
    "fault": {
        "faultstring": &quot;XMLToJSON[policy_name]: String can not be assigned to message type.",
        "detail": {
            "errorcode": "steps.xml2json.InCompatibleTypes"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "XMLToJSON[XMLToJSON_CheckType]: String can not be assigned to message type.",
        "detail": {
            "errorcode": &quot;steps.xml2json.InCompatibleTypes"
        }
    }
}

原因

如果 <Source> 元素中定義的變數類型與 <OutputVariable> 元素不同,就會發生這個錯誤。請注意,<Source> 元素和 <OutputVariable> 元素所含的變數類型必須相符。

The valid types are message and string.

診斷

  1. 找出發生錯誤的 XML 到 JSON 政策。您可以在錯誤回應的 faultstring 元素中找到這個值。例如,在以下 faultstring 中,政策名稱為 XMLToJSON_CheckType

    "faultstring": "XMLToJSON[XMLToJSON_CheckType]: String can not be assigned to message type."
    
  2. 在失敗的 XML 至 JSON 政策中,記下 <OutputVariable> 中指定的值。

    以下是缺少 <OutputVariable> 元素的 XMLToJSON 政策範例

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    XMLToJSON async="false" continueOnError="fal>se&qu<ot; enabled>="true" n<ame="XM>LToJS<ON_CheckTyp>e&quo<t;
       > Displ<ayNameX>MLToJ<SON_CheckType/>Display<Name
        Proper>ties/<
        F>ormatgoogle/Fo<rmat
      > < OutputVar>iablerequest/OutputVariable
        SourceTrackingNumber/Source
    /XMLToJSON
    
  3. 決定 <Source><OutputVariable> 元素中指定的變數類型:

    1. 找出 API Proxy 套件中的程式碼,其中會先定義這些變數。
    2. 找出先定義及填入變數的政策後,您需要依下列方式判斷該變數的類型:
      1. 檢查 type 屬性值 (如有)。
      2. 如果沒有 type 屬性,系統會將變數視為字串。
    3. 如果 <Source> 中指定的變數類型為 string,而 <OutputVariable> 的類型為 message,或反之,則為錯誤原因。如要瞭解常見變數及其類型,請參閱變數參考資料

    舉例來說,請考慮使用「指派訊息」政策,將值設為名為 TrackingNumber 的變數,如下所示:

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    AssignMessage async="false" continueOnError="false>"<; enabled=&>quot;true" name=<"Assign>_Trac<kingNumber&>quot;<
        DisplayNa>meAssign_<Trac>kingNumber/Dis<playN>ame
        P<roper><ties/
      <  As>signVariable
      <     > N><ameTra>ckingNumb<er/N>ame
     <       Value![C>DATA[<Code560075393539898/Code]>]/Va<lue
            Ref/
        /Assi>gnVar<iable
        IgnoreUnresolvedVariablestrue/IgnoreUnresolvedVar>i<ables
        Assi>gnTo createNew="false" transport="http" type="request"/
    /AssignMessage
    

    請注意,透過 <AssignVariable> 設定的變數類型是字串。因此變數 TrackingNumber 的型別為字串。

    現在,請回想一下,XMLToJSON 政策的 <Source> 元素使用 TrackingNumber 變數:

    <Source>TrackingNumber</Source>
    

    同樣地,請回想一下,request 變數會用於 XML 到 JSON 政策的 <OutputVariable> 元素:

    <OutputVariable>request</OutputVariable>
    

    由於 TrackingNumber 的類型為 string,而 response 變數的類型為 message,因此兩者不相容,因此您會收到錯誤代碼:

    steps.xml2json.InCompatibleTypes
    

    如果 <Source> 元素中的變數為 message 類型,但 <OutputVariable> 元素中的變數為字串類型,也會發生上述錯誤。

解析度

請確認 <Source> 元素和 <OutputVariable> 元素中定義的變數類型一律相同。請注意,<Source> 元素和 <OutputVariable> 元素所含的變數類型必須相符。

如要修正上述的 XML 至 JSON 政策,您可以使用指派訊息政策宣告另一個 TrackingNumber_output 變數,該變數的類型為 string,並在 XML 至 JSON 政策的 <OutputVariable> 元素中使用這個變數。

修改過的指派訊息政策:

<?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
AssignMessage async="false" continueOnError="false>"<; enabled=&>quot;true" name=<"Assign>_Trac<kingNumber&>quot;<
    DisplayNa>meAssign_<Trac>kingNumber/Dis<playN>ame
    P<roper><ties/
  <  As>signVa<riabl>e<>/span>
<      >  NameTra<ckin>gNumb<er/Name
       > Valu<e![CDATA[Code5>60098/Cod<e]]/>Value
        Ref/
  <  /As>signVaria<ble<>/span>
    A<ssignVariable
 >     <  NameTrackingNumber_outp>ut/N<ame
        Ref/
    /Assi>gnVar<iable
    IgnoreUnresolvedVariablestrue/IgnoreUnresolvedVar>i<ables
    Assi>gnTo createNew="false" transport="http" type="request"/
/AssignMessage

修改過的 XMLToJSON 政策:

  <?xml version="1.0" encoding="UTF-8&quo>t; <standalone="yes"?
  XMLToJSON async="false" continueOnError="fal>se"<; enabled=&>quot;true" nam<e="XMLT>oJSON_C<heckType&qu>ot;
   <   Dis>playNa<meXMLTo>JSON_Ch<eckType/Displa>yName
      Propertie<s/
      Format>google/<Format>
      OutputV<ariable>Tra<ckingNumbe>r_output/OutputVariable
      SourceTrackingNumber/Source
  /XMLToJSON

InvalidSourceType

錯誤代碼

steps.xml2json.InvalidSourceType

錯誤回應主體

{
    "fault": {
        "faultstring";: "XMLToJSON[class invalid_class]: Invalid source type class invalid_class. Valid source types are [message, string].",
        "detail": {
            "errorcode": "steps.xml2json.InvalidSourceType"
        }
    }
}

錯誤訊息範例

{
    "fault": {
        "faultstring": "XMLToJSON[class java.lang.Integer]: Invalid source type class java.lang.Integer. Valid source types are [message, string].",
        "detail": {
            "errorcode": &quot;steps.xml2json.InvalidSourceType"
        }
    }
}

原因

如果用來定義 <Source> 元素的變數類型無效,就會發生這個錯誤。有效的變數類型為 messagestring

診斷

  1. 找出 XML 到 JSON 政策中使用的無效來源類型。您可以在錯誤訊息中找到這項資訊。舉例來說,在下列錯誤中,無效的類型為整數。

    "faultstring": "XMLToJSON[class java.lang.Integer]: Invalid source type class java.lang.Integer. Valid source types are [message, string]."
    
  2. 請檢查發生失敗的特定 API Proxy 中,所有 XML 到 JSON 政策。在 XML 轉換為 JSON 政策失敗的情況下,請注意 <Source> 中指定的變數名稱。

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    XMLToJSON async="false" continueOnError="fal>se&qu<ot; enabled>="true" n<ame="XM>LToJS<ON_CheckTyp>e&quo<t;
       > Displ<ayNameX>MLToJ<SON_CheckType/>DisplayN<ame
        Propert>ies/
    <    Fo>rmatgoog<le/Form>a<t
        Outp>utVariableresponse/OutputVariable
        SourceBookCode/Source
    /XMLToJSON
    
  3. 決定 <Source> 元素中指定的變數類型:

    1. 在 API Proxy 組合中找出最先定義這個變數的程式碼。
    2. 找到先定義及填入變數的政策後,您必須依下列方式判斷該變數的類型:
      1. 檢查 type 屬性值 (如有)。
      2. 如果沒有類型屬性,則系統會將變數視為字串。
    3. 如果 <Source> 中指定的變數類型既不是 message 也不是 string 類型,則會導致錯誤。如要瞭解常見變數和它們的類型,請參閱變數參考資料

    舉例來說,請考量 ExtractVariables 政策,用於從 XML 酬載擷取值,並將值設為 BookCode 變數,以便成為 integer 類型,如下所示:

    <?xml version="1.0" encoding="UTF-8&quo>t<; standalone="yes"?
    ExtractVariables async="false" continueOnError=">false<" enab>led="true&q<uot; name=&q>uot;E<xtract_Book>Code&<quot;<>/span>
        Dis<playNam>eExtr<act_BookCode/DisplayName
        Properties/>
        Sour<cerequest/Source
        XMLPayload stopPay>loadProcessin<g=&qu>ot;false"<
         >   Variab<le name=&>quot;<BookCode&qu>o<t; type="int>eger"
                XPath/root/BookCode/XPath
            /Variable
        /XMLPayload
    /ExtractVariables
    

    請回想一下,BookCode 變數是在 XML 到 JSON 政策的 <Source> 元素中使用:

    <Source>BookCode</Source>
    

    由於這個變數的類型為 Integer,而非有效的 <Source> 類型,因此 API Proxy 會失敗,並顯示以下錯誤訊息:

    steps.xml2json.InvalidSourceType
    

解析度

請確認用來指定 <Source> 元素的變數類型是否有效。有效的 <Source> 類型為 messagestring

為避免 XML 轉 JSON 政策發生上述錯誤,您可以使用 request 變數,該變數的類型為 message,或任何其他有效的 XML 酬載字串。