Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python查找xls标记和他的孩子_Python_Xml_Xslt_Xpath_Elementtree - Fatal编程技术网

用python查找xls标记和他的孩子

用python查找xls标记和他的孩子,python,xml,xslt,xpath,elementtree,Python,Xml,Xslt,Xpath,Elementtree,我在xls代码文件中找到一个特定的标签,然后和他的孩子一起拿到,这让我有些麻烦 例如: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-en

我在xls代码文件中找到一个特定的标签,然后和他的孩子一起拿到,这让我有些麻烦

例如:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:crossFunction="http://bla.bla.bla/" xmlns:simple-date-format="xalan://java.text.SimpleDateFormat" xmlns:srv="bla.bla.bla1" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" xmlns:date="http://exslt.org/dates-and-times" xmlns:customCoreFunction="http://bla.bla.bla2" xmlns:xalan="http://xml.apache.org/xalan" xmlns:productCoreFunction="http://bla.bla.bla" xmlns:srvesb0="http://esb.original.com.br/HistoricoComentario" xmlns:exsl="http://exslt.org/common" version="1.0" exclude-result-prefixes="xbla.bla.bla"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>  
      <xsl:variable name="uriTokenSeparator" select="';'"/>  
      <xsl:variable name="uriKeyValueSeparator" select="'='"/>  
      <xsl:template match="/"> 
        <xsl:variable name="messageContext" select="."/>  
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
          <soapenv:Header></soapenv:Header>  
          <soapenv:Body> 
            <xsl:element name="srvesb0:getHistory"> 
              <xsl:if test="((/soap:Envelope/soap:Body/*/note[@name='note']/catchId) and ((/soap:Envelope/soap:Body/*/note[@name='note']/catchId!='') or (/soap:Envelope/soap:Body/*/note[@name='note']/catchId/@*)))"> 
                <xsl:element name="srvesb0:idCapture"> 
                  <xsl:value-of select="/soap:Envelope/soap:Body/*/note[@name='note']/catchId"/> 
                </xsl:element> 
              </xsl:if> 
            </xsl:element> 
          </soapenv:Body> 
        </soapenv:Envelope> 
      </xsl:template> 
    </xsl:stylesheet>
但如果某个用户尝试迭代某个特定标记,则会出现任何结果:

    import lxml.etree as XT


xslt = XT.parse('transformation.xsl')
rootxslt = xslt.getroot()

for child in rootxslt:
    child.tag = child.tag.split('}', 1)[1]  # strip all namespaces
    print child.tag, child.attrib, child.text
    for child2 in child:
        child2.tag = child2.tag.split('}', 1)[1]  # strip all namespaces
        print child2.tag, child2.attrib, child2.text
        for child3 in child2:
            child3.tag = child3.tag.split('}', 1)[1]  # strip all namespaces
            print child3.tag, child3.text, child3.text
            for child4 in child3:
                child4.tag = child4.tag.split('}', 1)[1]  # strip all namespaces
                print child4.tag, child4.text, child4.text
                for child5 in child4:
                    child5.tag = child5.tag.split('}', 1)[1]  # strip all namespaces
                    print child5.tag, child5.text, child5.text
    import lxml.etree as XT


xslt = XT.parse('transformation.xsl')
rootxslt = xslt.getroot()

for child in rootxslt.findall("Body"):
    child.tag = child.tag.split('}', 1)[1]  # strip all namespaces
    print child.tag, child.attrib, child.text
    for child2 in child:
        child2.tag = child2.tag.split('}', 1)[1]  # strip all namespaces
        print child2.tag, child2.attrib, child2.text
        for child3 in child2:
            child3.tag = child3.tag.split('}', 1)[1]  # strip all namespaces
            print child3.tag, child3.text, child3.text
            for child4 in child3:
                child4.tag = child4.tag.split('}', 1)[1]  # strip all namespaces
                print child4.tag, child4.text, child4.text
                for child5 in child4:
                    child5.tag = child5.tag.split('}', 1)[1]  # strip all namespaces
                    print child5.tag, child5.text, child5.text
有人知道我怎样才能从“尸体”标签上得到这棵树吗


谢谢

您可以使用
xpath
方法来实现这一点

>>> import lxml.etree as XT
>>> xslt = XT.parse('c:/scratch/sample.xml')
>>> xslt.xpath('.//soapenv:Body', namespaces={'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/'})
[<Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x5e60c88>]
>>> theBody[0]
<Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x5e60c88>
>>> list(theBody[0].iterdescendants())
[<Element {http://www.w3.org/1999/XSL/Transform}element at 0x5e60b08>, <Element {http://www.w3.org/1999/XSL/Transform}if at 0x5e7ea08>, <Element {http://www.w3.org/1999/XSL/Transform}element at 0x5e7ea48>, <Element {http://www.w3.org/1999/XSL/Transform}value-of at 0x5e7ea88>]
编辑2:和另一个,使用BeautifulSoup。如果您使用
'xml'
作为美化组的第二个参数,那么您可以很容易地解析名称空间元素,正如我刚刚从中了解到的

导入bs4 >>>soup=bs4.BeautifulSoup(打开('sample.xml').read(),'xml') >>>body=汤。查找所有('body')) >>>身体 [ ] >>>正文[0]。查找('value-of')
您可以使用
xpath
方法来实现这一点

>>> import lxml.etree as XT
>>> xslt = XT.parse('c:/scratch/sample.xml')
>>> xslt.xpath('.//soapenv:Body', namespaces={'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/'})
[<Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x5e60c88>]
>>> theBody[0]
<Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x5e60c88>
>>> list(theBody[0].iterdescendants())
[<Element {http://www.w3.org/1999/XSL/Transform}element at 0x5e60b08>, <Element {http://www.w3.org/1999/XSL/Transform}if at 0x5e7ea08>, <Element {http://www.w3.org/1999/XSL/Transform}element at 0x5e7ea48>, <Element {http://www.w3.org/1999/XSL/Transform}value-of at 0x5e7ea88>]
编辑2:和另一个,使用BeautifulSoup。如果您使用
'xml'
作为美化组的第二个参数,那么您可以很容易地解析名称空间元素,正如我刚刚从中了解到的

导入bs4 >>>soup=bs4.BeautifulSoup(打开('sample.xml').read(),'xml') >>>body=汤。查找所有('body')) >>>身体 [ ] >>>正文[0]。查找('value-of')
谢谢@Bill Bell它可以工作了!但是,通过这种方式,我需要在代码中设置名称空间:'namespaces={'soapenv':''}',或'//soapenv:'。有没有一种方法可以将xpath用于标记“Body”而忽略以前的名称空间?我很抱歉,据我所知没有。为什么需要避免设置名称空间?因为名称空间可能会发生变化,这取决于所使用的XML。我发现了类似的东西。//*[local-name()='NamedTag'],但它返回了SyntaxError:invalid predicate error我已经建议了另一种方法。谢谢@Bill Bell它可以工作了!但是,通过这种方式,我需要在代码中设置名称空间:'namespaces={'soapenv':''}',或'//soapenv:'。有没有一种方法可以将xpath用于标记“Body”而忽略以前的名称空间?我很抱歉,据我所知没有。为什么需要避免设置名称空间?因为名称空间可能会发生变化,这取决于所使用的XML。我发现了类似的东西。//*[local-name()='NamedTag'],但它返回SyntaxError:invalid predicate error我建议了另一种方法。
>>> import lxml.etree as XT
>>> xslt = XT.parse('sample.xml')
>>> sibling = xslt.xpath('.//xsl:variable[@name="messageContext"]', namespaces={'xsl': "http://www.w3.org/1999/XSL/Transform"})
<Element {http://schemas.xmlsoap.org/soap/envelope/}Envelope at 0x11912c8>
>>> envelope = sibling[0].getnext()
>>> list(envelope.iter())
[<Element {http://schemas.xmlsoap.org/soap/envelope/}Envelope at 0x11912c8>, <Element {http://schemas.xmlsoap.org/soap/envelope/}Header at 0x1191448>, <Element {http://schemas.xmlsoap.org/soap/envelope/}Body at 0x1191248>, <Element {http://www.w3.org/1999/XSL/Transform}element at 0x11910c8>, <Element {http://www.w3.org/1999/XSL/Transform}if at 0x1191488>, <Element {http://www.w3.org/1999/XSL/Transform}element at 0x11914c8>, <Element {http://www.w3.org/1999/XSL/Transform}value-of at 0x1191508>]
>>> import bs4
>>> soup = bs4.BeautifulSoup(open('sample.xml').read(), 'xml')
>>> body = soup.find_all('Body')
>>> body
[<Body>
<xsl:element name="srvesb0:getHistory">
<xsl:if test="((/soap:Envelope/soap:Body/*/note[@name='note']/catchId) and ((/soap:Envelope/soap:Body/*/note[@name='note']/catchId!='') or (/soap:Envelope/soap:Body/*/note[@name='note']/catchId/@*)))">
<xsl:element name="srvesb0:idCapture">
<xsl:value-of select="/soap:Envelope/soap:Body/*/note[@name='note']/catchId"/>
</xsl:element>
</xsl:if>
</xsl:element>
</Body>]
>>> body[0].find('value-of')
<xsl:value-of select="/soap:Envelope/soap:Body/*/note[@name='note']/catchId"/>