幾個月以來,我一直在嘗試標準化 SEC 檔案。但是,我已經意識到每年每家公司的 us-gaap 標簽都有不同的含義。
因此,我現在的目標是從每個 us-gaap 子項的 cal.xml 檔案中提取父項。
AAPL 檔案 2011-09-24的cal.xml 檔案示例:子項“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?
提前謝謝你們。請讓我知道如何改進我的問題。
uj5u.com熱心網友回復:
只需xpath為您打算決議的節點部分指定一個需要。每個檔案,默認是第一級./*:
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、 及其子loc或 的屬性calculationArc,請考慮 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/342352.html
上一篇:數字標簽<影像>XSLT/XML
