简体   繁体   English

用于删除XML元素名称的正则表达式

[英]Regular expression to delete XML element names

I have a situation. 我有情况 In order to develop one quite complex XML, I have used "place-holders". 为了开发一种非常复杂的XML,我使用了“占位符”。 Once my XML is ready, I need to delete those 'place-holders'. 准备好XML之后,我需要删除那些“占位符”。

Sample Input 样本输入

<consumers>
  <place-holder_1>
    <consumer>
      <val>1</val>
    </consumer>
  </place-holder_1>
  <place-holder_2>
    <consumer-info>
      <val>2</val>
    </consumer-info>
  </place-holder_2>
</consumers>

Sample Output 样本输出

<consumers>
  <consumer>
    <val>1</val>
  </consumer>
  <consumer-info>
    <val>2</val>
  </consumer-info>
</consumers>

Basically, I am looking for a regex which can delete all tags containing anything with "place-holder" in a generic way. 基本上,我正在寻找一个正则表达式,它可以以通用方式删除所有包含带有“ place-holder”的标签。 Any number between 1 to 10 can be suffix of 'place-holder' tag. 1至10之间的任何数字都可以是'place-holder'标签的后缀。

I am struggling to come up with regex for this. 我正在为此想出正则表达式。

The following regex captures the desired nodes 以下正则表达式捕获所需的节点

^\\s*<\\/?place-holder_\\d{1,2}>

Once captured, you can replace the first capturing group with empty string. 捕获后,您可以将第一个捕获组替换为空字符串。

or you can use : 或者您可以使用:

(?=(<\/?place-holder_(10|\d)>))

you can test it here! 您可以在这里进行测试

You should be able to use XSLT in kettle . 您应该能够在水壶中使用XSLT

XSLT 1.0 XSLT 1.0

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

  <!--Identity Transform (https://www.w3.org/TR/xslt#copying)-->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[starts-with(local-name(),'place-holder')]">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

Working Example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM