跳转至

Parse XML as schema(将 XML 解析为架构 (Parse XML as schema))

Supported in: Batch, Streaming

Parses xml strings following the given schema definition, ignoring any fields not in the schema.

Expression categories: File, Struct

Declared arguments

  • Input schema: Schema definition used when parsing the xml strings.
    Type\
  • Xml: The xml string to parse.
    Expression\
  • optional Attribute prefix: Prefix for attributes on tags.
    Literal\
  • optional Ignore namespace: If set, ignores the namespace on elements and attributes. For example, \ would be treated as if it were just . Defaults to false.
    Literal\
  • optional Output mode: The 'simple' output mode will return null if an error is encountered during parsing (including those found as a result of invalid input). The 'with errors' output mode will return a parsable struct with any errors found during parsing in an 'errors' field and a valid parsed xml in the 'ok' field.
    Enum\
  • optional Value tag: The tag used for the value when there are attributes in the element having no child.
    Literal\

Output type: Struct

Examples

Example 1: Base case

Argument values:

  • Input schema: Struct\>
  • Xml: xml
  • Attribute prefix: null
  • Ignore namespace: null
  • Output mode: SIMPLE
  • Value tag: null
xml Output
\
 \XB-112\
 \
  \JFK\
  \2000\
 \

\
{
airport: {
id: JFK,
miles: 2000,
},
id: XB-112,
}

Example 2: Base case

Description: When namespace is ignored, parsing ignores namespace in the data. Note that namespaces in the schema will never match a key since the namespace is filtered.

Argument values:

  • Input schema: Struct\>
  • Xml: xml
  • Attribute prefix: _
  • Ignore namespace: true
  • Output mode: SIMPLE
  • Value tag: null
xml Output
\
\John Doe\
\john.doe@exampl...
{
_attribute: tall,
address: {
city: Exampleville,
 *...

Example 3: Null case

Description: When a requested field is missing in the input XML the field becomes null.

Argument values:

  • Input schema: Struct\>
  • Xml: xml
  • Attribute prefix: null
  • Ignore namespace: null
  • Output mode: SIMPLE
  • Value tag: null
xml Output
\
 \XB-112\
 \
  \JFK\
 \

\
{
airport: {
id: JFK,
miles: null,
},
id: XB-112,
}

Example 4: Null case

Description: When the requested schema is too small, only the fields in the schema are parsed.

Argument values:

  • Input schema: Struct\
  • Xml: xml
  • Attribute prefix: _
  • Ignore namespace: null
  • Output mode: SIMPLE
  • Value tag: null
xml Output
\
 \XB-112\
 \
  \JFK\
 \

\
{
id: XB-112,
}

Example 5: Null case

Description: You can read attributes by putting attribute prefix in front of the name.

Argument values:

  • Input schema: Struct\<id:String, airport:Struct<_id:String, miles:Integer>>
  • Xml: xml
  • Attribute prefix: _
  • Ignore namespace: null
  • Output mode: SIMPLE
  • Value tag: null
xml Output
\ \XB-112\ \
  \2000\
 \

\
{
airport: {
_id: JFK,
miles: 2000,
},
id: XB-112,
}


中文翻译


将 XML 解析为架构 (Parse XML as schema)

支持:批处理 (Batch)、流处理 (Streaming)

根据给定的架构定义解析 XML 字符串,忽略架构中不包含的任何字段。

表达式类别: 文件 (File)、结构体 (Struct)

声明的参数

  • 输入架构 (Input schema): 解析 XML 字符串时使用的架构定义。
    类型\
  • Xml: 要解析的 XML 字符串。
    表达式\
  • 可选 属性前缀 (Attribute prefix): 标签上属性的前缀。
    字面量\
  • 可选 忽略命名空间 (Ignore namespace): 如果设置,则忽略元素和属性上的命名空间。例如,\ 将被视为 \。默认为 false。
    字面量\
  • 可选 输出模式 (Output mode): '简单 (simple)' 输出模式在解析过程中遇到错误(包括因无效输入导致的错误)时将返回 null。'包含错误 (with errors)' 输出模式将返回一个可解析的结构体,其中包含解析过程中发现的任何错误(位于 'errors' 字段)以及有效的已解析 XML(位于 'ok' 字段)。
    枚举\
  • 可选 值标签 (Value tag): 当元素有属性但没有子元素时,用于值的标签。
    字面量\

输出类型: Struct

示例

示例 1:基本情况

参数值:

  • 输入架构: Struct\>
  • Xml: xml
  • 属性前缀: null
  • 忽略命名空间: null
  • 输出模式: SIMPLE
  • 值标签: null
xml 输出
\
 \XB-112\
 \
  \JFK\
  \2000\
 \

\
{
airport: {
id: JFK,
miles: 2000,
},
id: XB-112,
}

示例 2:基本情况

描述: 当忽略命名空间时,解析会忽略数据中的命名空间。请注意,由于命名空间已被过滤,架构中的命名空间永远不会匹配到键。

参数值:

  • 输入架构: Struct\>
  • Xml: xml
  • 属性前缀: _
  • 忽略命名空间: true
  • 输出模式: SIMPLE
  • 值标签: null
xml 输出
\
\John Doe\
\john.doe@exampl...
{
_attribute: tall,
address: {
city: Exampleville,
 *...

示例 3:空值情况

描述: 当输入 XML 中缺少请求的字段时,该字段变为 null。

参数值:

  • 输入架构: Struct\>
  • Xml: xml
  • 属性前缀: null
  • 忽略命名空间: null
  • 输出模式: SIMPLE
  • 值标签: null
xml 输出
\
 \XB-112\
 \
  \JFK\
 \

\
{
airport: {
id: JFK,
miles: null,
},
id: XB-112,
}

示例 4:空值情况

描述: 当请求的架构过小时,仅解析架构中包含的字段。

参数值:

  • 输入架构: Struct\
  • Xml: xml
  • 属性前缀: _
  • 忽略命名空间: null
  • 输出模式: SIMPLE
  • 值标签: null
xml 输出
\
 \XB-112\
 \
  \JFK\
 \

\
{
id: XB-112,
}

示例 5:空值情况

描述: 您可以通过在名称前添加属性前缀来读取属性。

参数值:

  • 输入架构: Struct\<id:String, airport:Struct<_id:String, miles:Integer>>
  • Xml: xml
  • 属性前缀: _
  • 忽略命名空间: null
  • 输出模式: SIMPLE
  • 值标签: null
xml 输出
\ \XB-112\ \
  \2000\
 \

\
{
airport: {
_id: JFK,
miles: 2000,
},
id: XB-112,
}