【问题标题】:How do I alter a XML-File based on the content of tags如何根据标签的内容更改 XML 文件
【发布时间】:2022-01-20 08:24:10
【问题描述】:

我有超过 3k 行的 XML 格式文件。

结构总是相同的,并在重复中与此相对应:

...
<Placemark>
        <name> Text</name>
        <description><![CDATA[]]></description>
        <Point>
            <coordinates>x, y, z</coordinates>
        </Point>
        <Style>
            <IconStyle>
                <Icon>
                    icon
                </Icon>
            </IconStyle>
        </Style>
</Placemark>
...

我想根据来自namedescription 的信息更改Style 块,因此如果出现某个单词,请将现有的Style 块替换为某个其他Style 块。将有少于 20 个不同的Style 块和“触发词”。整个事情应该为每个文件运行一次。

如果我必须编写代码,使用哪种语言和使用哪些框架? 或者您知道可以执行此操作的应用程序吗?

【问题讨论】:

    标签: xml xml-parsing


    【解决方案1】:

    听起来像是 XSLT 的工作。您可以从身份转换开始,然后为生成不同 Style 内容的每个“触发词”添加专门的模板,具体取决于 namedescription 是否具有任何“触发词”。

    带有“triggerWord”和“otherTriggerWord”的专用Style模板的示例XSLT:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        
      <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
        
      <xsl:template match="Placemark[(name,description)[contains(., 'triggerWord')]]/Style">
        <Style>
          <!--custom style content for triggerWord-->
        </Style>
      </xsl:template>
        
        
      <xsl:template match="Placemark[(name,description)[contains(., 'otherTriggerWord')]]/Style">
        <Style>
          <!--custom style content for otherTriggerWord-->
        </Style>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多