修改XML文件 - 划分值

时间:2017-09-12 13:07:47

标签: python xml lxml sld

我希望通过划分它们来修改xml文件中的值。

我需要将onActivityResultse:SvgParameter[name="stroke-width"]的值除以3.6。我在Python.org上发现了这个教程,Modifying an XML File,但它似乎不起作用,我在这里找不到类似的东西。任何帮助或观点将不胜感激!

这里有一些示例XML,但我希望能够在包含多个文件的文件夹中输出脚本并将其转换为所有文件。

XML:

se:Size

代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.1.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns:se="http://www.opengis.net/se">
  <NamedLayer>
    <se:Name>QGIS_SLD_ScaleProblem</se:Name>
    <se:Description>
      <se:Title>QGIS_SLD_ScaleProblem</se:Title>
      <se:Abstract>This SLD is in Pixels, but QGIS will read it as mm</se:Abstract>
    </se:Description>
    <UserStyle>
      <se:Name>QGIS_SLD_ScaleProblem</se:Name>
      <se:FeatureTypeStyle>
        <se:Rule>
          <se:Name>QGIS_SLD_ScaleProblem_Line</se:Name>
          <se:LineSymbolizer>
            <se:Name>Line</se:Name>
            <se:Stroke>
              <se:SvgParameter name="stroke">#FF0000</se:SvgParameter>
              <se:SvgParameter name="stroke-width">36</se:SvgParameter>
            </se:Stroke>
          </se:LineSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>QGIS_SLD_ScaleProblem_Point</se:Name>
          <se:PointSymbolizer>
            <se:Name>Point</se:Name>
            <se:Graphic>
             <se:Mark>
              <se:WellKnownName>circle</se:WellKnownName>
              <se:Fill>
               <se:SvgParameter name="fill">#FF0000</se:SvgParameter>
              </se:Fill>
             </se:Mark>
             <se:Size>36</se:Size>
            </se:Graphic>
          </se:PointSymbolizer>
        </se:Rule>
      </se:FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

答案我看过:

1 个答案:

答案 0 :(得分:0)

考虑XSLT用于转换XML文件的专用语言。 Python的lxml模块可以运行XSLT 1.0脚本。另外,XSLT可以在Python之外运行,因此具有可移植性。

只需调用identity transform按原样复制整个文档,然后在选定节点上运行除法。这种方法不需要for循环或if逻辑。

XSLT (另存为.xsl文件,一个格式正确的XML文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:se="http://www.opengis.net/se">                               
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="se:SvgParameter[@name='stroke-width']|se:Size">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:value-of select="format-number(text(), '#') div 3.6"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Python (在循环中迭代多个文件的进程)

import lxml.etree as et

# LOAD XML AND XSL
doc = et.parse('Input.xml')
xsl = et.parse('XSLTScript.xsl')

# TRANSFORM
transform = et.XSLT(xsl)
result = transform(doc)

# OUTPUT TO SCREEN    
print(result)

# OUTPUT TO FILE
with open('Output.xml', 'wb') as f:
    f.write(result)

<强>输出

<?xml version="1.0"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:se="http://www.opengis.net/se" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd">
  <NamedLayer>
    <se:Name>QGIS_SLD_ScaleProblem</se:Name>
    <se:Description>
      <se:Title>QGIS_SLD_ScaleProblem</se:Title>
      <se:Abstract>This SLD is in Pixels, but QGIS will read it as mm</se:Abstract>
    </se:Description>
    <UserStyle>
      <se:Name>QGIS_SLD_ScaleProblem</se:Name>
      <se:FeatureTypeStyle>
        <se:Rule>
          <se:Name>QGIS_SLD_ScaleProblem_Line</se:Name>
          <se:LineSymbolizer>
            <se:Name>Line</se:Name>
            <se:Stroke>
              <se:SvgParameter name="stroke">#FF0000</se:SvgParameter>
              <se:SvgParameter name="stroke-width">10</se:SvgParameter>
            </se:Stroke>
          </se:LineSymbolizer>
        </se:Rule>
        <se:Rule>
          <se:Name>QGIS_SLD_ScaleProblem_Point</se:Name>
          <se:PointSymbolizer>
            <se:Name>Point</se:Name>
            <se:Graphic>
              <se:Mark>
                <se:WellKnownName>circle</se:WellKnownName>
                <se:Fill>
                  <se:SvgParameter name="fill">#FF0000</se:SvgParameter>
                </se:Fill>
              </se:Mark>
              <se:Size>10</se:Size>
            </se:Graphic>
          </se:PointSymbolizer>
        </se:Rule>
      </se:FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>