我在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>
我只需要获取“Body”标记中的代码:
<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>
然后,逐元素迭代并得到它的属性。你知道吗
但我用的任何搜索码都有用 .xpath文件 伊特尔先生 。查找
如果迭代.getroot(),结果将显示:
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
方法来实现这一点。你知道吗找到所需代码的容器后,如图所示,可以遍历容器的子体。你知道吗
编辑:另一种方法。你知道吗
假设:(1)一个名称空间将用于各种
xsl
文档,如下面的代码所示。(2) 一个xsl:variable
messageContext
将立即出现在您想要的任何内容之前。你知道吗然后从查找该内容前面的
messageContext
元素开始。现在找到这个旁边的元素。最后,遍历消息内容的body
的后代。或者不管你想做什么。你知道吗编辑2:和另一个,使用BeautifulSoup。如果使用
'xml'
作为美化组的第二个参数,那么就可以很容易地解析名称空间元素,正如我刚刚从https://stackoverflow.com/a/35564127/131187学到的那样。你知道吗相关问题 更多 >
编程相关推荐