Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove string wrapped in double square brackets [[abc]]

XML:

   <root>
    <a>This is first para [[the first para1]]</a>
    <a>This is second para [[the second para2]]</a>
   </root>

XSLT 2.0 I tried:

   <xsl:template match='@* | node()'>
        <xsl:copy>
          <xsl:apply-templates select='@* | node()'/>
        </xsl:copy>        
  </xsl:template>
  <xsl:template match="*[contains(text(),'[[')]"></xsl:template>

Expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  <root>
   <a> This is first para</a>
   <a> This is second para</a>
  </root>

Thank you in advance for help.

>Solution :

This could do it for you in XSLT 2.0. Please note that in your sample input, the second text entry is missing the finishing ‘]]’.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  exclude-result-prefixes="#all">
  
  <xsl:template match='@* | node()'>
    <xsl:copy>
      <xsl:apply-templates select='@* | node()'/>
    </xsl:copy>        
  </xsl:template>
  
  <xsl:template match="text()">
    <xsl:sequence select="replace(., ' [\[][\[].*\]\]', '')" />
  </xsl:template>
  
</xsl:stylesheet>

In the event that the missing ‘]]’ in the second entry is intentional, you could make the ending ‘]]’ optional in the match pattern:

  <xsl:template match="text()">
    <xsl:sequence select="replace(., '[\[][\[].*(\]\])?', '')" />
  </xsl:template>

This latter provides this output with your given input:

<root>
  <a>This is first para </a>
  <a>This is second para </a>
</root>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading