سؤال

My requirement is like

Input :-

<request> <attribute> <attributeName>Name</attributeName> <attributeValue>a &amp; b</attributeValue> </attribute> <attribute> <attributeName>Name1</attributeName> <attributeValue>b</attributeValue> </attribute> </request>

Output:-

<request> <attribute> <attributeName>Name</attributeName> <attributeValue>a & b</attributeValue> </attribute> <attribute> <attributeName>Name1</attributeName> <attributeValue>b</attributeValue> </attribute> </request>

Escape characters can come in n number of tags and i need to replace all at run time as attribute element type is unbounded. How can i achieve the same in xslt???

هل كانت مفيدة؟

المحلول

This produces the output you want! (Let me know if it doesn't!)

The identity template, copies all elements.

The ampText template, looks for all text containing &amp;

The letters template iterates (recursively) over all letters in the text, and replaces all instances of &amp; with &.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 >
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

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

<xsl:template name="ampText" match="text()[contains(.,'&amp;')]">
    <xsl:call-template name="letters">
        <xsl:with-param name="text" select="." />
    </xsl:call-template>
</xsl:template>

<xsl:template name="letters">
  <xsl:param name="text" select="'Some text'" />
  <xsl:if test="$text != ''">
    <xsl:variable name="letter" select="substring($text, 1, 1)" />
    <xsl:choose>
        <xsl:when test="$letter = '&amp;'">
            <xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$letter" /></xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="letters">
      <xsl:with-param name="text" select="substring-after($text, $letter)" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top