删除所有 XML 父级,并保留具有特定子级的 XML 父级(正则表达式)

问题描述 投票:0回答:2

我很想删除每个不包含特定值的父级,例如

<drawableDictionary>l_njdocklod12</drawableDictionary>

下面使用的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<CMapTypes name="lodetnjdock_wh1">
    <extensions />
    <archetypes>
        <Item type="CBaseArchetypeDef">
            <lodDist value="550" />
            <flags value="0" />
            <specialAttribute value="0" />
            <bbMin x="-12.3383" y="-6.42445" z="-13.3852" />
            <bbMax x="12.3382" y="6.42445" z="13.3852" />
            <bsCentre x="-5.05447E-05" y="2.38419E-06" z="0" />
            <bsRadius value="19.3046" />
            <hdTextureDist value="0" />
            <name>hash_349C91C8</name>
            <textureDictionary>njdocklod04</textureDictionary>
            <clipDictionary />
            <drawableDictionary>lodnjdocksuper11_grp</drawableDictionary>
            <physicsDictionary>nj_docks</physicsDictionary>
            <assetType>ASSET_TYPE_DRAWABLEDICTIONARY</assetType>
            <assetName>hash_349C91C8</assetName>
            <extensions />
        </Item>
        <Item type="CBaseArchetypeDef">
            <lodDist value="550" />
            <flags value="0" />
            <specialAttribute value="0" />
            <bbMin x="-5.6393" y="-5.45088" z="-4.9861" />
            <bbMax x="5.6393" y="5.45088" z="4.9861" />
            <bsCentre x="0" y="-4.76837E-07" z="4.76837E-07" />
            <bsRadius value="9.29382" />
            <hdTextureDist value="0" />
            <name>hash_46EE366B</name>
            <textureDictionary>njdocklod12</textureDictionary>
            <clipDictionary />
            <drawableDictionary>l_njdocklod12</drawableDictionary>
            <physicsDictionary>nj_docks</physicsDictionary>
            <assetType>ASSET_TYPE_DRAWABLEDICTIONARY</assetType>
            <assetName>hash_46EE366B</assetName>
            <extensions />
        </Item>
        <Item type="CBaseArchetypeDef">
            <lodDist value="550" />
            <flags value="0" />
            <specialAttribute value="0" />
            <bbMin x="-11.8993" y="-34.8797" z="-4.27972" />
            <bbMax x="11.8992" y="34.8796" z="4.29434" />
            <bsCentre x="-2.76566E-05" y="-1.52588E-05" z="0.0073123" />
            <bsRadius value="37.102" />
            <hdTextureDist value="0" />
            <name>hash_52EAB9C8</name>
            <textureDictionary>njdocklod10</textureDictionary>
            <clipDictionary />
            <drawableDictionary>lodnjdocksuper05_grp</drawableDictionary>
            <physicsDictionary>nj_docks</physicsDictionary>
            <assetType>ASSET_TYPE_DRAWABLEDICTIONARY</assetType>
            <assetName>hash_52EAB9C8</assetName>
            <extensions />
        </Item>
    </archetypes>
    <name>nj_docks_lod</name>
    <dependencies />
    <compositeEntityTypes itemType="CCompositeEntityType" />
</CMapTypes>

想要的结果

<?xml version="1.0" encoding="UTF-8"?>
<CMapTypes name="lodetnjdock_wh1">
    <extensions />
    <archetypes>
        <Item type="CBaseArchetypeDef">
            <lodDist value="550" />
            <flags value="0" />
            <specialAttribute value="0" />
            <bbMin x="-5.6393" y="-5.45088" z="-4.9861" />
            <bbMax x="5.6393" y="5.45088" z="4.9861" />
            <bsCentre x="0" y="-4.76837E-07" z="4.76837E-07" />
            <bsRadius value="9.29382" />
            <hdTextureDist value="0" />
            <name>hash_46EE366B</name>
            <textureDictionary>njdocklod12</textureDictionary>
            <clipDictionary />
            <drawableDictionary>l_njdocklod12</drawableDictionary>
            <physicsDictionary>nj_docks</physicsDictionary>
            <assetType>ASSET_TYPE_DRAWABLEDICTIONARY</assetType>
            <assetName>hash_46EE366B</assetName>
            <extensions />
        </Item>
    </archetypes>
    <name>nj_docks_lod</name>
    <dependencies />
    <compositeEntityTypes itemType="CCompositeEntityType" />
</CMapTypes>

我想使用正则表达式使用Notepad++来实现这一点,因为我离我的舒适区很远,到目前为止这对我有用。

regex xml notepad++
2个回答
3
投票

这项工作的工具是 XSLT。这是一个非常简单的转换:复制所有内容不变的身份模板加上删除不需要的项目的规则:

<xsl:template match="Item[not(drawableDictionary='l_njdocklod12')]"/>

在 XSLT 3.0 中,您可以通过编写来获取身份模板

<xsl:mode on-no-match="shallow-copy"/>

在旧版本中,写

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

0
投票

您可以搜索所有不包含该值的

<Item>
元素
l_njdocklod12
使用这个正则表达式:
  <Item((?!l_njdocklod12).)*?</Item>$

请注意,

(?!
是向前看,以验证您的字符串不存在。
我使用
*?
进行非贪婪匹配。

确保选中

. matches newline

要删除找到的匹配项,请将其替换为空字符串。

© www.soinside.com 2019 - 2024. All rights reserved.