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

ni65a41a  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(119)

此问题来自超级用户migrated,因为3天前可以在Stack Overflow. Migrated上回答。
我想删除每个不包含特定值的父对象,例如

<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>

我想使用正则表达式,使用记事本++存档,因为我离我的舒适区很远,到目前为止,这对我很有效。

nle07wnf

nle07wnf1#

这项工作的工具是XSLT,它是一个非常简单的转换:复制所有未更改内容的身份模板以及删除不需要项目的规则:

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

在XSLT3.0中,可以通过编写以下代码来获取身份模板

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

在旧版本中,请写入

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

mzaanser2#

您可以使用以下正则表达式搜索所有不包含值l_njdocklod12<Item>元素:
<Item((?!l_njdocklod12).)*?</Item>$
注意,(?!是一个前瞻函数,用于验证您的字符串是否不存在。
我使用*?进行非贪婪匹配。
确保选中. matches newline
要删除找到的匹配项,请将其替换为空字符串。

相关问题