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

您目前查看的是 ApigeeApigee Hybrid 說明文件。
查看 Apigee Edge 說明文件。

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> 元素中指定的訊息或字串變數符合下列任一條件,就會發生這項錯誤:

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

舉例來說,如果 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 政策會在 response 元素中指定名為 response 的變數,與 faultstring 中的內容相符:<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/>
        <Format>google</Format>
        <OutputVariable>response</OutputVariable>
        <Source>response</Source>
    </XMLToJSON>
    
  3. 判斷 <Source> 元素中使用的變數是否已定義,且可在執行 XML 至 JSON 政策的流程中使用。

  4. 如果變數為下列任一值:

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

    這就是錯誤原因。

    舉例來說,假設上述 XML 轉 JSON 政策應在「要求」流程中執行。請注意,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 to 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://your_host_alias/v1/testxmltojson" -H "Content-Type: application/xml"
    

    其中 your_host_alias 是用於存取 API 的公開網域,已在覆寫檔案的 virtualhosts.hostAliases 屬性中設定。請參閱「指定設定覆寫」。

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

    steps.xml2json.ExecutionFailed
    

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

解析度

請確認透過 <Source> 元素傳遞至 XML to JSON 政策的輸入內容是有效的 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://your_host_alias/v1/testxmltojson" -H "Content-Type: application/xml" -X POST -d @company.xml
    

    其中 your_host_alias 是用於存取 API 的公開網域,已在覆寫檔案的 virtualhosts.hostAliases 屬性中設定。請參閱「指定設定覆寫」。

原因:輸入內容無效或格式有誤

如果 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 政策範例中,下列無效的 XML 是透過 city.xml 檔案傳遞至「擷取變數」政策:

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

    以下是範例 API 呼叫,說明如何傳遞要求:

    curl -v "http://your_host_alias/v1/testxmltpjson" -H "Content-Type: application/xml" -X POST -d @city.xml
    

    其中 your_host_alias 是用於存取 API 的公開網域,已在覆寫檔案的 virtualhosts.hostAliases 屬性中設定。請參閱「指定設定覆寫」。

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

    steps.xml2json.ExecutionFailed
    

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

解析度

請確認透過 <Source> 元素傳遞至 XML to 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": "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" standalone="yes"?>
    <XMLToJSON async="false" continueOnError="false" enabled="true" name="Convert-XMLToJSON">
        <DisplayName>Convert-XMLToJSON</DisplayName>
        <Properties/>
        <Format>google</Format>
        <Source>TrackingNumber</Source>
    </XMLToJSON>
    
    
  3. 判斷 <Source> 元素中指定的變數類型:

    1. 在 API Proxy 套件中找出程式碼,其中變數是先定義的。
    2. 找出變數定義和填入的政策後,請按照下列步驟判斷變數類型:
      1. 檢查類型屬性的值 (如有)。
      2. 如果沒有 type 屬性,變數會視為字串。
    3. 如果變數的型別為 string,就是導致錯誤的原因。如要瞭解常見變數及其類型,請參閱「變數參考資料」。

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

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign_TrackingNumber">
        <DisplayName>Assign_TrackingNumber</DisplayName>
        <Properties/>
        <AssignVariable>
            <Name>TrackingNumber</Name>
            <Value><![CDATA[<Code>560075393539898</Code>]]></Value>
            <Ref/>
        </AssignVariable>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
        <AssignTo createNew="false" transport="http" type="request"/>
    </AssignMessage>
    

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

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

    <Source>TrackingNumber</Source>
    

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

    steps.xml2json.OutputVariableIsNotAvailable
    

解析度

請確認如果 XMLToJSON 政策 <Source> 元素中指定的變數屬於字串類型,則 <OutputVariable> 元素在此情況下為必要元素。

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

<?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>TrackingNumber</Source>
</XMLToJSON>

InCompatibleTypes

錯誤代碼

steps.xml2json.InCompatibleTypes

錯誤回應主體

{
    "fault": {
        "faultstring": "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": "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" standalone="yes"?>
    <XMLToJSON async="false" continueOnError="false" enabled="true" name="XMLToJSON_CheckType">
        <DisplayName>XMLToJSON_CheckType</DisplayName>
        <Properties/>
        <Format>google</Format>
        <OutputVariable>request</OutputVariable>
        <Source>TrackingNumber</Source>
    </XMLToJSON>
    
  3. 判斷 <Source><OutputVariable> 元素中指定的變數類型:

    1. 在 API Proxy 套件中找出程式碼,其中會先定義每個變數。
    2. 找出變數定義和填入的政策後,請按照下列步驟判斷變數類型:
      1. 檢查類型屬性的值 (如有)。
      2. 如果沒有 type 屬性,變數會視為字串。
    3. 如果 <Source> 中指定的變數類型為 string,而 <OutputVariable> 的類型為訊息,反之亦然,就會導致錯誤。如要瞭解常見變數及其類型,請參閱「變數參考資料」。

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

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign_TrackingNumber">
        <DisplayName>Assign_TrackingNumber</DisplayName>
        <Properties/>
        <AssignVariable>
            <Name>TrackingNumber</Name>
            <Value><![CDATA[<Code>560075393539898</Code>]]></Value>
            <Ref/>
        </AssignVariable>
        <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
        <AssignTo createNew="false" transport="http" type="request"/>
    </AssignMessage>
    

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

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

    <Source>TrackingNumber</Source>
    

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

    <OutputVariable>request</OutputVariable>
    

    由於 TrackingNumberstring 類型,而 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" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign_TrackingNumber">
    <DisplayName>Assign_TrackingNumber</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>TrackingNumber</Name>
        <Value><![CDATA[<Code>560098</Code>]]></Value>
        <Ref/>
    </AssignVariable>
    <AssignVariable>
        <Name>TrackingNumber_output</Name>
        <Ref/>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

修改後的 XMLToJSON 政策:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <XMLToJSON async="false" continueOnError="false" enabled="true" name="XMLToJSON_CheckType">
      <DisplayName>XMLToJSON_CheckType</DisplayName>
      <Properties/>
      <Format>google</Format>
      <OutputVariable>TrackingNumber_output</OutputVariable>
      <Source>TrackingNumber</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": "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" standalone="yes"?>
    <XMLToJSON async="false" continueOnError="false" enabled="true" name="XMLToJSON_CheckType">
        <DisplayName>XMLToJSON_CheckType</DisplayName>
        <Properties/>
        <Format>google</Format>
        <OutputVariable>response</OutputVariable>
        <Source>BookCode</Source>
    </XMLToJSON>
    
  3. 判斷 <Source> 元素中指定的變數類型:

    1. 在 API Proxy 套件中找出程式碼,這個變數最初是在這裡定義。
    2. 找出變數定義和填入的政策後,請按照下列步驟判斷變數類型:
      1. 檢查類型屬性的值 (如有)。
      2. 如果沒有 type 屬性,變數會視為字串。
    3. 如果 <Source> 中指定的變數類型既不是 message 也不是 string 類型,就會導致錯誤。如要瞭解常見變數及其類型,請參閱「變數參考資料」。

    舉例來說,假設您使用 ExtractVariables 政策從 XML 酬載擷取值,並將值設為變數 BookCode,且類型為 integer,如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract_BookCode">
        <DisplayName>Extract_BookCode</DisplayName>
        <Properties/>
        <Source>request</Source>
        <XMLPayload stopPayloadProcessing="false">
            <Variable name="BookCode" type="integer">
                <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 酬載字串。