如何使用 pd.read_xml 正确解析 SEC cal.xml 文件?

How to parse SEC cal.xml files correctly with pd.read_xml?

几个月以来,我一直在尝试标准化 SEC 文件。但是,我意识到 us-gaap 标签每年对每个公司都有不同的含义。

因此,我现在的目标是从每个 us-gaap 子项的 cal.xml 文件中提取父项。

AAPL 文件 2011-09-24 的 cal.xml file 示例: 子项“AccountsPayableCurrent”的父项似乎是“LiabilitiesCurrent”。

我想使用 pandas.read_xml 功能。 df = pd.read_xml('https://www.sec.gov/Archives/edgar/data/320193/000119312511282113/aapl-20110924_cal.xml')

但是,生成的 df 没有我可以提取此类信息的形式。 有人知道如何为我希望它做的每个 ca.xml 自动做吗?

我读过 pd.read_xml 的文档,它可以将样式表 (XSLT) 作为参数。是否有可能从 .xml 或相关的 .xsd?

创建这样的 XSLT

提前谢谢大家。请告诉我如何改进我的问题。

只需将需要的 xpath 指定到您要解析的节点部分。每docs,默认为第一级./*:

import pandas as pd
import requests

url = (
    "https://www.sec.gov/Archives/edgar/data/320193/"
    "000119312511282113/aapl-20110924_cal.xml"
)
hdr = {
    "user-agent": 
    (
       "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) "
       "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 "
       "Mobile Safari/537.36"
    )
}

r = requests.get(url, headers=hdr)

# roleRef NODES
roleRef_df = pd.read_xml(
    r.text,
    xpath = "//doc:roleRef",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"}
)

# calculationLink NODES
calculationLink_df = pd.read_xml(
    r.text,
    xpath = "//doc:calculationLink",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"}
)

# loc NODES
loc_df = pd.read_xml(
    r.text,
    xpath = "//doc:calculationLink/doc:loc",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"}
)

# calculationArc NODES
calculationArc_df = pd.read_xml(
    r.text,
    xpath = "//doc:calculationLink/doc:calculationArc",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"}
)

如果您需要更广泛的解析,例如检索父项 calculationLink 及其子项 loccalculationArc 的属性,请考虑 XSLT。

xsl = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:doc="http://www.xbrl.org/2003/linkbase">
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
     <xsl:copy>
       <xsl:apply-templates select="descendant::doc:loc"/>
       <xsl:apply-templates select="descendant::doc:calculationArc"/>
     </xsl:copy>
    </xsl:template>
    
    <xsl:template match="doc:loc|doc:calculationArc">
     <xsl:copy>
       <xsl:copy-of select="ancestor::doc:calculationLink/@*"/>
       <xsl:copy-of select="@*"/>
     </xsl:copy>
    </xsl:template>
</xsl:stylesheet>'''

calculationLink_loc_df = pd.read_xml(
    r.text,
    xpath = "//doc:loc",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"},
    stylesheet = xsl
)

calculationLink_arc_df = pd.read_xml(
    r.text,
    xpath = "//doc:calculationArc",
    namespaces = {"doc": "http://www.xbrl.org/2003/linkbase"},
    stylesheet = xsl
)

输出

calculationLink_loc_df.head()
#       type                                               role                                               href                                              label
# 0  locator  http://www.apple.com/taxonomy/role/StatementOf...  http://xbrl.fasb.org/us-gaap/2011/elts/us-gaap...                 us-gaap_CostOfGoodsAndServicesSold
# 1  locator  http://www.apple.com/taxonomy/role/StatementOf...  http://xbrl.fasb.org/us-gaap/2011/elts/us-gaap...                                us-gaap_GrossProfit
# 2  locator  http://www.apple.com/taxonomy/role/StatementOf...  http://xbrl.fasb.org/us-gaap/2011/elts/us-gaap...  us-gaap_IncomeLossFromContinuingOperationsBefo...
# 3  locator  http://www.apple.com/taxonomy/role/StatementOf...  http://xbrl.fasb.org/us-gaap/2011/elts/us-gaap...                    us-gaap_IncomeTaxExpenseBenefit
# 4  locator  http://www.apple.com/taxonomy/role/StatementOf...  http://xbrl.fasb.org/us-gaap/2011/elts/us-gaap...                              us-gaap_NetIncomeLoss


calculationLink_arc_df.head()

#   type                                               role                                          arcrole                                               from                                                 to  order  weight  priority       use
# 0  arc  http://www.apple.com/taxonomy/role/StatementOf...  http://www.xbrl.org/2003/arcrole/summation-item                                us-gaap_GrossProfit                            us-gaap_SalesRevenueNet   1.01     1.0         2  optional
# 1  arc  http://www.apple.com/taxonomy/role/StatementOf...  http://www.xbrl.org/2003/arcrole/summation-item                                us-gaap_GrossProfit                 us-gaap_CostOfGoodsAndServicesSold   1.02    -1.0         2  optional
# 2  arc  http://www.apple.com/taxonomy/role/StatementOf...  http://www.xbrl.org/2003/arcrole/summation-item  us-gaap_IncomeLossFromContinuingOperationsBefo...                        us-gaap_OperatingIncomeLoss   1.07     1.0         2  optional
# 3  arc  http://www.apple.com/taxonomy/role/StatementOf...  http://www.xbrl.org/2003/arcrole/summation-item  us-gaap_IncomeLossFromContinuingOperationsBefo...                  us-gaap_NonoperatingIncomeExpense   1.08     1.0         2  optional
# 4  arc  http://www.apple.com/taxonomy/role/StatementOf...  http://www.xbrl.org/2003/arcrole/summation-item                              us-gaap_NetIncomeLoss  us-gaap_IncomeLossFromContinuingOperationsBefo...   1.09     1.0         2  optional