DOM node.textContent parsing and replacing

In the foreach below, I need to parse $ node.TextContent for a keyword and wrap bold labels around it. Is this possible with the DOM? How?

$myContent ="<h1>This word should not be replaced: TEST</h1>. But this one should be replaced: test";

$dom = new DOMDocument;
$dom->loadHTML(strtolower($myContent));
$xPath = new DOMXPath($dom);
foreach($xPath->query("//text()[contains(.,'test') and not(ancestor::h1)]") as $node)
    {
        /*need to do a replace on each occurrence of the word "test"
         in $node->textContent here so that it becomes <b>test</b>. How? */
    }

echo $dom->saveHTMLshould give:

<h1>This word should not be replaced: TEST</h1>. 
But this one should be replaced: <b>test</b>"
+3
source share
1 answer

Change . As @LarsH noted, I did not pay attention to the requirement that the replacement be in bold.

There are two easy ways to fix this:

0.1. In the conversion, replace :

  <xsl:value-of select="$pRep"/>

with

  <b><xsl:value-of select="$pRep"/></b>

0.2. Pass as a parameter value pReplacementnot only "ABC" , but <b>ABC</b>

Convert XSLT 1.0 :

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

 <xsl:param name="pTarget" select="'test'"/>
 <xsl:param name="pReplacement" select="'ABC'"/>

 <xsl:variable name="vCaps"     select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
 <xsl:variable name="vLowecase" select="'abcdefghijklmnopqrstuvwxyz'"/>

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

 <xsl:template match="text()[not(ancestor::h1)]">
  <xsl:call-template name="replaceCI">
   <xsl:with-param name="pText" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="replaceCI">
  <xsl:param name="pText"/>
  <xsl:param name="pTargetText" select="$pTarget"/>
  <xsl:param name="pRep" select="$pReplacement"/>

  <xsl:variable name="vLowerText"
       select="translate($pText, $vCaps, $vLowecase)"/>
  <xsl:choose>
   <xsl:when test=
   "not(contains($vLowerText, $pTargetText))">
     <xsl:value-of select="$pText"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:variable name="vOffset" select=
    "string-length(substring-before($vLowerText, $pTargetText))"/>

    <xsl:value-of select="substring($pText,1,$vOffset)"/>

    <xsl:value-of select="$pRep"/>

    <xsl:call-template name="replaceCI">
     <xsl:with-param name="pText" select=
     "substring($pText, $vOffset + string-length($pTargetText)+1)"/>
     <xsl:with-param name="pTargetText" select="$pTargetText"/>
     <xsl:with-param name="pRep" select="$pRep"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

XML- (, ):

<html>
<h1>This word should not be replaced: TEST</h1>.
 But this one should be replaced: test
</html>

:

<html>
<h1>This word should not be replaced: TEST</h1>.
 But this one should be replaced: ABC
</html>

:

  • , .

  • , , .

  • XSLT 2.0.

+1

Source: https://habr.com/ru/post/1773330/


All Articles