1

私はXMLの初心者です。lxml python libsを使用してSAMLドキュメントを処理していますが、私の質問は実際にはSAMLまたはSSOに関連していません。

簡単に言うと、このXMLドキュメントに存在する「ds」名前空間に属するすべての要素を削除する必要があります。Xpath Searchを調べ、findall()を調べましたが、名前空間の操作方法がわかりません。

元のドキュメントは次のようになります。

<Response IssueInstant="dateandtime" ID="redacted" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <saml:Issuer>redacted.com</saml:Issuer>
  <Status>
    <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
  </Status>
  <saml:Assertion Version="2.0" IssueInstant="redacted" ID="redacted">
    <saml:Issuer>redacted</saml:Issuer>
    <ds:Signature>
      <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
        <ds:Reference URI="#redacted">
          <ds:Transforms>
            <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
          </ds:Transforms>
          <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
          <ds:DigestValue>redacted</ds:DigestValue>
        </ds:Reference>
      </ds:SignedInfo>
      <ds:SignatureValue>redacted==</ds:SignatureValue>
      <ds:KeyInfo>
        <ds:X509Data>
          <ds:X509Certificate>certificateredacted=</ds:X509Certificate>
        </ds:X509Data>
        <ds:KeyValue>
          <ds:RSAKeyValue>
            <ds:Modulus>modulusredacted==</ds:Modulus>
            <ds:Exponent>AQAB</ds:Exponent>
          </ds:RSAKeyValue>
        </ds:KeyValue>
      </ds:KeyInfo>
    </ds:Signature>
    <saml:Subject>
      <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">subject_redacted</saml:NameID>
      <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
        <saml:SubjectConfirmationData NotOnOrAfter="date_time_redacted" Recipient="https://website.com/redacted"/>
      </saml:SubjectConfirmation>
    </saml:Subject>
    <saml:Conditions NotOnOrAfter="date_time_redacted" NotBefore="date_time_redacted">
      <saml:AudienceRestriction>
        <saml:Audience>audience_redacted</saml:Audience>
      </saml:AudienceRestriction>
    </saml:Conditions>
    <saml:AuthnStatement AuthnInstant="date_time_redacted" SessionIndex="date_time_redacted">
      <saml:AuthnContext>
        <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef>
      </saml:AuthnContext>
    </saml:AuthnStatement>
    <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified" Name="attribute_name_redacted">
        <saml:AttributeValue xsi:type="xs:string">attribute=redacted</saml:AttributeValue>
      </saml:Attribute>
      <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified" Name="attribute_name_redacted">
        <saml:AttributeValue xsi:type="xs:string">value_redacted</saml:AttributeValue>
      </saml:Attribute>
    </saml:AttributeStatement>
  </saml:Assertion>
</Response>

私が欲しいのは、次のようなドキュメントです。

<Response IssueInstant="dateandtime" ID="redacted" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <saml:Issuer>redacted.com</saml:Issuer>
  <Status>
    <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
  </Status>
  <saml:Assertion Version="2.0" IssueInstant="redacted" ID="redacted">
    <saml:Issuer>redacted</saml:Issuer>
    <saml:Subject>
      <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">subject_redacted</saml:NameID>
      <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
        <saml:SubjectConfirmationData NotOnOrAfter="date_time_redacted" Recipient="https://website.com/redacted"/>
      </saml:SubjectConfirmation>
    </saml:Subject>
    <saml:Conditions NotOnOrAfter="date_time_redacted" NotBefore="date_time_redacted">
      <saml:AudienceRestriction>
        <saml:Audience>audience_redacted</saml:Audience>
      </saml:AudienceRestriction>
    </saml:Conditions>
    <saml:AuthnStatement AuthnInstant="date_time_redacted" SessionIndex="date_time_redacted">
      <saml:AuthnContext>
   <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef>
      </saml:AuthnContext>
    </saml:AuthnStatement>
    <saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified" Name="attribute_name_redacted">
        <saml:AttributeValue xsi:type="xs:string">attribute=redacted</saml:AttributeValue>
      </saml:Attribute>
      <saml:Attribute NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified" Name="attribute_name_redacted">
        <saml:AttributeValue xsi:type="xs:string">value_redacted</saml:AttributeValue>
      </saml:Attribute>
    </saml:AttributeStatement>
  </saml:Assertion>
</Response>
4

2 に答える 2

2

次のように、 XPathを使用して名前空間内の要素を見つけることができます//namespace:*

doc_root.xpath('//ds:*', namespaces={'ds': 'http://www.w3.org/2000/09/xmldsig#'})

したがって、この名前空間のすべての子を削除するには、次のようなものを使用できます。

def strip_dsig(doc_root):
    nsmap={'ds': 'http://www.w3.org/2000/09/xmldsig#'}
    for element in doc_root.xpath('//ds:*', namespaces=nsmap):
        element.getparent().remove(element)
    return doc_root
于 2012-06-28T18:13:35.770 に答える
0

これは、xslスタイルシートを使用すると非常に簡単に実行できます。これはおそらくあなたの最善のアプローチです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
exclude-result-prefixes="ds">
<!-- no_ds.xsl -->

<xsl:template match="node()|@*">
  <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>

<xsl:template match="ds:*"><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="@ds:*"/>    
</xsl:stylesheet> 

これは、 xsltproc(libxml2の場合)または同等のものを使用してコマンドラインから実行できます。

xsltproc -o directoryname/ no_ds.xsl file1.xml file2.xml

これにより、ds名前空間なしでdirectoryname/file1.xmlおよびdirectoryname/file2.xmlが作成されます。

lxmlのlibxslt2バインディングを使用してlxmlでこれを行うこともできます。

no_ds_stylesheet = etree.parse('no_ds.xsl')
no_ds_transform  = etree.XSLT()

# doc_to_transform is an Element or ElementTree
# from etree.fromstring(), etree.XML(), or etree.parse()
no_ds_doc = no_ds_transform(doc_to_transform)

#no_ds_doc is now another ElementTree doc, the result of the XSLT transform.
#You can reuse the no_ds_transform object multiple times (and should if you can)

no_ds_doc2 = no_ds_transform(doc_to_transform2)

XSLTドキュメントはXMLドキュメントであるため、lxmlを使用してカスタムXSLTスタイルシートをその場で作成し、動的に省略したい名前空間を定義することもできます。(読者のための演習として残してください。)

于 2012-06-28T18:04:06.893 に答える