0

我想生成一个类似这样的 XML:

<linkbase>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/Generalinformationaboutreportinginstitution">
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_GeneralInformationAboutReportingInstitutionAbstract" xlink:label="GeneralInformationAboutReportingInstitutionAbstract" xlink:title="GeneralInformationAboutReportingInstitutionAbstract"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_DateOfQuarterEnded" xlink:label="DateOfQuarterEnded" xlink:title="DateOfQuarterEnded"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="GeneralInformationAboutReportingInstitutionAbstract" xlink:to="DateOfQuarterEnded" xlink:title="presentation: GeneralInformationAboutReportingInstitutionAbstract to DateOfQuarterEnded" order="2.0"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_NameOfReportingInstitution" xlink:label="NameOfReportingInstitution" xlink:title="NameOfReportingInstitution"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="GeneralInformationAboutReportingInstitutionAbstract" xlink:to="NameOfReportingInstitution" xlink:title="presentation: GeneralInformationAboutReportingInstitutionAbstract to NameOfReportingInstitution" use="optional" order="1.0"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_DateOfReport" xlink:label="DateOfReport" xlink:title="DateOfReport"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="GeneralInformationAboutReportingInstitutionAbstract" xlink:to="DateOfReport" xlink:title="presentation: GeneralInformationAboutReportingInstitutionAbstract to DateOfReport" order="3.0"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_ValidationStatus" xlink:label="ValidationStatus" xlink:title="ValidationStatus"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="GeneralInformationAboutReportingInstitutionAbstract" xlink:to="ValidationStatus" xlink:title="presentation: GeneralInformationAboutReportingInstitutionAbstract to ValidationStatus" order="6.0"/>
  </presentationLink>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/SignatoryInformation">
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_SignatoryAbstract" xlink:label="SignatoryAbstract" xlink:title="SignatoryAbstract"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_NameOfSignatory" xlink:label="NameOfSignatory" xlink:title="NameOfSignatory"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="SignatoryAbstract" xlink:to="NameOfSignatory" xlink:title="presentation: SignatoryAbstract to NameOfSignatory" order="1.0"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_DesignationOfSignatory" xlink:label="DesignationOfSignatory" xlink:title="DesignationOfSignatory"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="SignatoryAbstract" xlink:to="DesignationOfSignatory" xlink:title="presentation: SignatoryAbstract to DesignationOfSignatory" order="2.0"/>
    <loc xlink:type="locator" xlink:href="../core/in-rbi-rep.xsd#in-rbi-rep_PlaceOfSignature" xlink:label="PlaceOfSignature" xlink:title="PlaceOfSignature"/>
    <presentationArc xlink:type="arc" xlink:arcrole="http://www.xbrl.org/2003/arcrole/parent-child" xlink:from="SignatoryAbstract" xlink:to="PlaceOfSignature" xlink:title="presentation: SignatoryAbstract to PlaceOfSignature" order="3.0"/>
  </presentationLink>
</linkbase>

我能够生成<presentationLink>标签,但无法按顺序生成内部标签。我想我需要遍历<presentationLink>标签中的“订单”属性,但是如何。以上是我从与问题相关的大型 XML 中获取的摘要。我当前的代码如下所示:

from openpyxl import load_workbook ### The tags come from an excel file
from lxml import etree
import xml.etree.ElementTree as xml

wb = load_workbook("t.xlsx")
ws = wb.worksheets[4]
m_row = ws.max_row
m_col = ws.max_column 

xlink = "http://www.w3.org/1999/xlink"
link = "http://www.xbrl.org/2003/linkbase"
role_prefix = "http://www.rbi.org/in/xbrl/2012-12-07/role/"
href_prefix = "in-rbi-alo-role.xsd#"
elem_type = ['extended', 'locator', 'resource', 'arc', 'simple']

ns = {None : link, 'xlink': xlink}
root = etree.Element("linkbase", nsmap = ns)
# print(root)
# print(root.tag)
print(etree.tostring(root, pretty_print=True).decode("utf-8"))

for i in range(1, m_row): 
    cell_objd = ws.cell(row = i+1, column = 4)
    cell_objf = ws.cell(row = i+1, column = 6)
    cell_objh = ws.cell(row = i+1, column = 8)
    if cell_objd.value == 'ELR':
        pl = etree.SubElement(root, "{" + link + "}presentationLink", attrib={"{" + xlink + "}type" : elem_type[0], "{" + xlink + "}role" : role_prefix + cell_objf.value})
        #print(cell_objd.value, cell_objf.value, cell_objh.value)
print(etree.tostring(root, pretty_print=True).decode("utf-8"))

上面的代码生成以下 XML:

<linkbase xmlns="http://www.xbrl.org/2003/linkbase" xmlns:xlink="http://www.w3.org/1999/xlink">
  <roleRef roleRef="http://www.rbi.org/in/xbrl/2012-12-07/role/Generalinformationaboutreportinginstitution" xlink:type="simple" xlink:href="in-rbi-alo-role.xsd#Generalinformationaboutreportinginstitution"/>
  <roleRef roleRef="http://www.rbi.org/in/xbrl/2012-12-07/role/ReportOnAssetsLiabilitiesAndOtherProducts" xlink:type="simple" xlink:href="in-rbi-alo-role.xsd#ReportOnAssetsLiabilitiesAndOtherProducts"/>
  <roleRef roleRef="http://www.rbi.org/in/xbrl/2012-12-07/role/AnnexureIDetailsOfOffBalanceSheetExposures" xlink:type="simple" xlink:href="in-rbi-alo-role.xsd#AnnexureIDetailsOfOffBalanceSheetExposures"/>
  <roleRef roleRef="http://www.rbi.org/in/xbrl/2012-12-07/role/AnnexureII" xlink:type="simple" xlink:href="in-rbi-alo-role.xsd#AnnexureII"/>
  <roleRef roleRef="http://www.rbi.org/in/xbrl/2012-12-07/role/Signatoryinformation" xlink:type="simple" xlink:href="in-rbi-alo-role.xsd#Signatoryinformation"/>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/Generalinformationaboutreportinginstitution"/>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/ReportOnAssetsLiabilitiesAndOtherProducts"/>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/AnnexureIDetailsOfOffBalanceSheetExposures"/>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/AnnexureII"/>
  <presentationLink xlink:type="extended" xlink:role="http://www.rbi.org/in/xbrl/2012-12-07/role/Signatoryinformation"/>
</linkbase>

如您所见,<presentationLink>已生成但我无法制作内部标签。我希望这个问题很清楚。如果需要,我可以提供生成 XML 所需的确切 excel。任何帮助表示赞赏。

谢谢。

4

0 回答 0