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> 元素中指定的消息或字符串变量发生以下任一情况,便会出现此错误:

  • 超出范围(在执行政策的特定流中不可用)
  • 无法解析(未定义)

例如,如果本应在请求流中执行 XML 到 JSON 政策,但将 <Source> 元素设置为 response 变量,而请求流中并不存在该变量,那么就会出现此错误。

诊断

  1. 标识出错的 JXML 到 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 政策本应在请求流中执行。如前文所述,XML 到 JSON 政策的 <Source> 元素中使用了 response 变量。响应变量仅在响应流中可用。

    由于请求流中不存在响应变量,因此会收到错误代码:

    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 到 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://<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. 验证 <Source> 元素中指定的传递给 XML 到 JSON 政策的输入是否是有效的 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": "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 代理软件包中找到代码。
    2. 确定首次在其中定义并填充变量的政策后,您需要按以下方式确定该变量的类型:
      1. 检查 type 特性的值(如果存在)。
      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 的类型为字符串。

    如前文所述,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" 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 代理软件包中找到代码。
    2. 确定首次在其中定义并填充变量的政策后,您需要按以下方式确定该变量的类型:
      1. 检查 type 特性的值(如果存在)。
      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>
    

    同样,如前文所述,XML 到 JSON 政策的 <OutputVariable> 元素中使用了 request 变量:

    <OutputVariable>request</OutputVariable>
    

    由于 TrackingNumber 的类型为 string,而 response 变量的类型为 message,它们是不兼容的类型,因此您将收到错误代码:

    steps.xml2json.InCompatibleTypes
    

    如果 <Source> 元素中的变量类型为 message,但 <OutputVariable> 元素中的变量类型为字符串,也可能会出现上述错误。

分辨率

确保 <Source> 元素和 <OutputVariable> 元素中定义的变量类型始终相同。<Source> 元素中包含的变量类型和 <OutputVariable> 元素必须完全匹配。

如需更正上面讨论的 XML 到 JSON 政策,您可以使用分配消息政策声明另一个类型为 string 的变量 TrackingNumber_output,并在 XML 到 JSON 政策的 <OutputVariable> 元素中使用此变量。

修改后的 AssignMessage 政策:

<?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 代理中的所有 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 代理软件包中找到代码。
    2. 确定首次在其中定义并填充变量的政策后,您需要按以下方式确定该变量的类型:
      1. 检查 type 特性的值(如果存在)。
      2. 如果 type 特性不存在,则变量将被视为字符串。
    3. 如果 <Source> 中指定的变量类型不是 messagestring 类型,则会导致错误原因。如需了解常见变量及其类型,请参阅变量参考文档

    例如,考虑一个 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>
    

    如前文所述,XML 到 JSON 政策的 <Source> 元素中使用了 BookCode 变量:

    <Source>BookCode</Source>
    

    由于此变量的类型为 Integer,它不是有效的 <Source> 类型,因此 API 代理会失败并显示以下错误:

    steps.xml2json.InvalidSourceType
    

分辨率

确保用于指定 <Source> 元素的变量类型有效。有效的 <Source> 类型为 messagestring

为避免 XML 到 JSON 政策出现上述错误,您可以使用类型为 messagerequest 变量或其他任何为有效 XML 载荷的字符串。