メインコンテンツまでスキップ

XMLのスキーマ

XML 文字列を解析し、そのスキーマを DDL 形式で推論します。

構文

Python
from pyspark.sql import functions as sf

sf.schema_of_xml(xml, options=None)

パラメーター

パラメーター

Type

説明

xml

pyspark.sql.Column または文字列

XML 文字列または XML 文字列を含む折りたたみ可能な文字列列。

options

辞書(オプション)

解析を制御するためのオプション。XML データ ソースと同じオプションを受け入れます。

戻り値

pyspark.sql.Column: 指定された XML から解析されたStructTypeの文字列表現。

例1 : 単一の要素を持つ単純なXMLの解析

Python
from pyspark.sql import functions as sf
df = spark.range(1)
df.select(sf.schema_of_xml(sf.lit('<p><a>1</a></p>')).alias("xml")).collect()
Output
[Row(xml='STRUCT<a: BIGINT>')]

例2 : 配列内の複数の要素を持つXMLの解析

Python
from pyspark.sql import functions as sf
df.select(sf.schema_of_xml(sf.lit('<p><a>1</a><a>2</a></p>')).alias("xml")).collect()
Output
[Row(xml='STRUCT<a: ARRAY<BIGINT>>')]

例3 : 属性を除外するオプションを使用してXMLを解析する

Python
from pyspark.sql import functions as sf
schema = sf.schema_of_xml('<p><a attr="2">1</a></p>', {'excludeAttribute':'true'})
df.select(schema.alias("xml")).collect()
Output
[Row(xml='STRUCT<a: BIGINT>')]

例4 : 複雑な構造を持つXMLの解析

Python
from pyspark.sql import functions as sf
df.select(
sf.schema_of_xml(
sf.lit('<root><person><name>Alice</name><age>30</age></person></root>')
).alias("xml")
).collect()
Output
[Row(xml='STRUCT<person: STRUCT<age: BIGINT, name: STRING>>')]

例5 : ネストされた配列を含むXMLの解析

Python
from pyspark.sql import functions as sf
df.select(
sf.schema_of_xml(
sf.lit('<data><values><value>1</value><value>2</value></values></data>')
).alias("xml")
).collect()
Output
[Row(xml='STRUCT<values: STRUCT<value: ARRAY<BIGINT>>>')]