How to replace node name with another in Xslt?

Bad wording on the question, sorry for that. I will try to explain what I'm trying to do. Basically I have a search result like Xml, and in this Xml there is a node like this:

<FIELD NAME="body">
  Somebody named 
  <key>Doris</key> 
  and 
  <key>Arnie</key> 
</FIELD>

In short, I need to replace "<key>" with "<strong>"; i.e. highlight the search queries (key node values ​​are what the user was looking for). In Xslt, I don’t know what the user was looking for, except for the Xml β†’ FIELD [@ name = 'body'] / key request.

Right now I have some kind of crazy code that will extract everything that is in front of the search term ("Doris"), but this works for 1 search term. We need to do this for several terms. The code we use looks like this:

  <xsl:template name="highlighter">
    <xsl:param name="text"/>
    <xsl:param name="what"/>

    <xsl:choose>
      <xsl:when test="contains($text, $what) and string-length($what) &gt; 0">
        <xsl:variable name="before" select="substring-before($text, $what)"/>
        <xsl:variable name="after" select="substring-after($text, $what)"/>
        <xsl:variable name="real-before" select="substring($text, 1, string-length($before))"/>
        <xsl:variable name="real-what" select="substring($text, string-length($before) + 1, string-length($what))"/>
        <xsl:variable name="real-after" select="substring($text, string-length($before) + string-length($what) + 1)"/>
        <xsl:value-of select="$real-before"/>

        <strong>
          <xsl:value-of select="$real-what"/>
        </strong>

        <xsl:call-template name="highlighter">
          <xsl:with-param name="text" select="$real-after"/>
          <xsl:with-param name="what" select="$what"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

, , - , . :

string body = doc.SelectSingleNode("FIELD[@NAME='body']");
NodeCollection nodes = doc.SelectNodes("FIELD[@NAME='body']/key");
foreach (var node in nodes) {
    body = hightlighter(body, node.InnerText);   
}

- XSLT, , ...;)

: ; , , :

Somebody named <strong>Doris</strong> and <strong>Arnie</strong>
+3
3

- , -. , , . :

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

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

, . apply .

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!-- Template to match your 'key' and replace with strong -->
    <xsl:template match="FIELD[@name='body']/key">
        <strong><xsl:apply-templates select="@*|node()"/></strong>
    </xsl:template>

    <!-- Template to match all nodes, copy them and then apply templates to children. -->
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
+2

Why can't you just replace the "KEY" element with the "STRONG" elements? It’s better not to think too strongly about it.

<xsl:template match="FIELD[@NAME='body']">
  <xsl:apply-templates/>
<xsl:template>

<xsl:template match="key">
  <strong>
  <xsl:apply-templates/>
  <strong>
</xsl:template>

<xsl:template match="text()">
  <xsl:copy-of select="."/>
</xsl:template>

Or did I misunderstand you?

+1
source

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


All Articles