Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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 编辑<;说明>;使用lxml创建KML_Python_Html_Lxml_Kml_Google Earth - Fatal编程技术网

Python 编辑<;说明>;使用lxml创建KML

Python 编辑<;说明>;使用lxml创建KML,python,html,lxml,kml,google-earth,Python,Html,Lxml,Kml,Google Earth,我想用一个新的格式化html替换KML描述标记中的html 我的kml具有以下结构: <html> <body> <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">

我想用一个新的格式化html替换KML描述标记中的html

我的kml具有以下结构:

<html>
 <body>
  <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">
   <document id="WATER_MAINLINE_trim" xsi:schemalocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd">
    <name>
     WATER_MAINLINE_trim
    </name>
    <open>
     1
    </open>
    <snippet maxlines="0">
    </snippet>
    <style id="LineStyle00">
     <LabelStyle>
            <color>00000000</color>
            <scale>0</scale>
        </LabelStyle>
        <LineStyle>
            <color>ff240087</color>
        </LineStyle>
        <PolyStyle>
            <color>00000000</color>
            <outline>0</outline>
        </PolyStyle>
    </style>
    <folder id="FeatureLayer0">
     <name>
      WATER_MAINLINE_trim
     </name>
     <open>
      1
     </open>
     <snippet maxlines="0">
     </snippet>
     <placemark id="ID_00000">
      <name>
       0100026491
      </name>
      <snippet maxlines="0">
      </snippet>
      <description>
       <meta content="text/html" http-equiv="Content-Type" />
       <meta content="text/html; charset=utf-8" http-equiv="content-type" />
       <table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px">
        <tr style="text-align:center;font-weight:bold;background:#9CBCE2">
         <td>
          0100026491
         </td>
        </tr>
        <tr>
         <td>
          <table style="font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px">
           <tr>
            <td>
             FID
            </td>
            <td>
             0
            </td>
           </tr>
           <tr bgcolor="#D4E4F3">
            <td>
             PRIKEY
            </td>
            <td>
             0100026491
            </td>
           </tr>
           <tr>
            <td>
             YEAR_INST
            </td>
            <td>
             2001
            </td>
           </tr>
           <tr bgcolor="#D4E4F3">
            <td>
             PIPE_CLASS
            </td>
            <td>
             PRIMARY
            </td>
           </tr>
           <tr>
            <td>
             DIAMETER
            </td>
            <td>
             1500
            </td>
           </tr>
           <tr bgcolor="#D4E4F3">
            <td>
             MATERIAL
            </td>
            <td>
             SP
            </td>
           </tr>
           <tr>
            <td>
             STATUS
            </td>
            <td>
             ACTIVE
            </td>
           </tr>
           <tr bgcolor="#D4E4F3">
            <td>
             BA
            </td>
            <td>
             FCOM
            </td>
           </tr>
           <tr>
            <td>
             SUBCLASS
            </td>
            <td>
             WATER MAINLINE
            </td>
           </tr>
          </table>
         </td>
        </tr>
       </table>
      </description>
     </placemark>
    </folder>
   </document>
  </kml>
 </body>
</html>

水管线配平
1.
00000000
0
ff240087
00000000
0
水管线配平
1.
0100026491
0100026491
氢火焰离子化检测器
0
普里基
0100026491
本年
2001
管道级
主要的,重要的
直径
1500
材料
服务提供商
地位
活跃的
文学士
FCOM
子类
输水干管
我有一个新的html:

newhtml="""<![CDATA[ \n<!------------TITLE SUBCLASS---------------->\n  <tr>\n    <td colspan="2" align="center">\n      <b><font color=\'#090259\' size=\'6\' style = \'bold\'>LA MESA BALARA</font><b>\n    </td>/n  </tr>\n<!------------IMAGE---------------->\n  <tr>\n    <td colspan="2" align="center">\n      <img src= http://static.rappler.com/images/640-lamesadam-20120728.jpg, width=500, height = 223, alt="picture" />\n    </td>\n  </tr>\n<!------------PRIKEY---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>PRIKEY</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>0100026491</p>\n    </td>\n<!------------YEAR INSTALLED---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Year Installed</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>2001</p>\n    </td>\n<!------------PIPE CLASS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Pipe Class</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>PRIMARY</p>\n    </td>\n<!------------DIAMETER---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Diameter (mm)</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>1500.000000</p>\n    </td>\n<!------------MATERIAL---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Material</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>SP</p>\n    </td>\n<!------------STATUS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Status</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>ACTIVE</p>\n    </td>\n<!------------BUSINESS ADDRESS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Business Address</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>Fairview-Commonwealth</p>\n    </td>]]>"""
newhtml=“”“\n\n\n LA MESA BALARA\n/n\n\n\n\n\n\nPRIKEY

\n\n\n0100026491

\n\n\n\n安装年份

\n\n\n2001

\n\n\n\n\n\n管道等级

\n\n\n\n\n\n\n主要管道直径(mm)

\n\n\n\n1500.000000

\n\n\n\n材料

\n\n\nSP

\n\n\n\n状态

\n\n\n活动

\n\n\n\n营业地址

\n\n\n\n\n\n\n美景联邦

\n]>“”
如何在使用lxml解析的kml中正确替换它,并且仍然是有效的kml?使用“valid”,我属于可以加载到Google Earth上的kml。我尝试使用BeautifulSoup进行替换,但我的outfile在加载到Google Earth时出错。它显示“意外元素”html“。所以我只想用lxml来代替它。任何帮助都将不胜感激。谢谢大家!

我有一个示例kml,其中包含5个线串位置标记


Trim.kML=

< P>因为KML是一个有效的XML文件,考虑到,专门用于修改XML文档和Python的LXML的转换语言可以运行XSLT 1脚本。p> 具体地说,下面的动态XSLT是从字符串解析的,首先运行标识转换以原样复制文档,然后用newhtml变量替换每次出现的

import lxml.etree as ET

# READ IN KML FILE
dom = ET.parse('trim.kml')

newhtml = """<![CDATA[\n<!------------TITLE SUBCLASS---------------->\n  <tr>\n    <td colspan="2" align="center">\n      <b><font color=\'#090259\' size=\'6\' style = \'bold\'>LA MESA BALARA</font><b>\n    </td>/n  </tr>\n<!------------IMAGE---------------->\n  <tr>\n    <td colspan="2" align="center">\n      <img src= http://static.rappler.com/images/640-lamesadam-20120728.jpg, width=500, height = 223, alt="picture" />\n    </td>\n  </tr>\n<!------------PRIKEY---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>PRIKEY</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>0100026491</p>\n    </td>\n<!------------YEAR INSTALLED---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Year Installed</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>2001</p>\n    </td>\n<!------------PIPE CLASS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Pipe Class</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>PRIMARY</p>\n    </td>\n<!------------DIAMETER---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Diameter (mm)</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>1500.000000</p>\n    </td>\n<!------------MATERIAL---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Material</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>SP</p>\n    </td>\n<!------------STATUS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Status</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>ACTIVE</p>\n    </td>\n<!------------BUSINESS ADDRESS---------------->\n  <tr>\n    <td bgcolor = \'#090259\', align="center" >\n      <p><font color = \'FFFFFF\', size =\'4\'>Business Address</p>\n    </td>\n \n    <td bgcolor = \'#d8d8ff\' align="center">\n      <p>Fairview-Commonwealth</p>\n    </td>]]>"""

# PARSE XSL FROM STRING
xslstr = '''<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ogc="http://www.opengis.net/ogc" xmlns:wfs="http://www.opengis.net/wfs">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

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

  <xsl:template match="description">
    <xsl:copy>
      <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
      <xsl:text disable-output-escaping="yes">{}</xsl:text>
      <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
    </xsl:copy>  
  </xsl:template>

</xsl:transform>'''.format(newhtml)

xslt = ET.fromstring(xslstr)

# TRANSFORM SOURCE TO NEW TREE
transform = ET.XSLT(xslt)
newdom = transform(dom)

# OUTPUT TO FILE
tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True, xml_declaration=True)

xmlfile = open('newTrim.kml','wb')
xmlfile.write(tree_out)
xmlfile.close()
将lxml.etree作为ET导入
#读入KML文件
dom=ET.parse('trim.kml'))
newhtml=“”“\n\n\n LA MESA BALARA\n/n\n\n\n\n\n\nPRIKEY

\n\n\n0100026491

\n\n\n\n安装年份

\n\n\n2001

\n\n\n\n\n\n管道等级

\n\n\n\n\n\n\n主要管道直径(mm)

\n\n\n\n1500.000000

\n\n\n\n材料

\n\n\nSP

\n\n\n\n状态

\n\n\n活动

\n\n\n\n营业地址

\n\n\n\n\n\n\n美景联邦

\n]>“” #从字符串解析XSL xslstr='' ![CDATA[ {} ]] ''。格式(新HTML) xslt=ET.fromstring(xslstr) #将源转换为新树 transform=ET.XSLT(XSLT) newdom=变换(dom) #输出到文件 tree\u out=ET.tostring(newdom,encoding='UTF-8',pretty\u print=True,xml\u declaration=True) xmlfile=open('newTrim.kml','wb') xmlfile.write(树输出) xmlfile.close()
请检查KML文档的名称空间。
xsi
前缀未在
根目录中声明(使其格式不正确)。我假设有一个复制错误:
。感谢您指出@Parfait。顺便说一句,如果您熟悉GIS,我是通过在ArcGIS上使用“Layer to kml”获得kml的。基本上是ArcGIS为该名称空间提供了这样的kml结构。再次感谢!嗨@Parfait!我收到一个错误:XMLSyntaxError:Document上schemaLocation的命名空间前缀xsi未定义,第3行第32列“当我使用您的代码时。我尝试学习XSLT和XML结构,但仍然无法使其工作。我可以获得更多帮助吗?非常感谢!这不是XSLT错误,而是解析源文件时出现的XML错误:
dom=ET.parse('trim.kml')
。请参阅我上面的评论。您的kml不是一个格式正确的XML文件。在
标记中使用
xsi
名称空间前缀之前,需要先声明它。