Link to home
Start Free TrialLog in
Avatar of tesmc
tesmcFlag for United States of America

asked on

XSL - Adding an attribute and renaming a tag?

I have the following input
<CommandRS Version="2003.XML1.0.1">
      <Response>MR - COLLECT&lt;X&gt; X IF SAME FOP OR OVERRIDE &lt;USD&gt;</Response>
</CommandRS>


I was assisted by an expert such that my xslt will process the above and translate it to = <Response> <X><CAD><X> </Response> using:

  <xsl:template match="Response">
    <xsl:copy>
      <xsl:call-template name="strip-between-tags">
        <xsl:with-param name="str" select="."/>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="strip-between-tags">
    <xsl:param name="str"/>
    <xsl:if test="contains($str, '&lt;')">
      <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
      <xsl:value-of select="substring-before(substring-after($str, '&lt;'), '&gt;')"></xsl:value-of>
      <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
      <xsl:call-template name="strip-between-tags">
        <xsl:with-param name="str" select="substring-after($str, '&gt;')"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="not (contains($str, '&lt;'))">
      <xsl:text disable-output-escaping="yes"> &lt;X&gt; </xsl:text>    
    </xsl:if>
  </xsl:template>

I understand this XML is not well formed, that wasn't the intent. I'm trying to built a specific message for a client.


However, I want my response to be

<CommandRQ Version="2003.XML1.0.1">
      <Request> <X><CAD><X> </Request>
</CommandRQ>



How do i add the attribute value inside a <CommandRQ> tag and replace the <Response> tag with <Request> ?
SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need a copy template XSLT with some special xsl:template's which do the replacement:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="CommandRS">
		<CommandRQ>
			<xsl:apply-templates select="*|@*"/>
		</CommandRQ>
	</xsl:template>

	<xsl:template match="Response">
		<Request>
			<xsl:apply-templates select="*|@*|text()"/>
		</Request>
	</xsl:template>

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

</xsl:stylesheet>

Open in new window

Note that z2c forgot the attribute Version in the first template.

Don't use the second template (for Response) from his solution since that might wipe out the parsing from the Response template you already have. Make sure you merge as I suggested

The other two templates are not necessary in your current setting, but they won't hurt. It is good policy to have them around anyway
Avatar of tesmc

ASKER

Gertone:

If I use suggestion :

  <xsl:template match="Response">
    <Request>
    <xsl:copy>
      <xsl:call-template name="strip-between-tags">
        <xsl:with-param name="str" select="."/>
      </xsl:call-template>
    </xsl:copy>  
    </Request>  
  </xsl:template>



this produces <CommandRQ Version="2003.XML1.0.1">
      <Request><Response> <X><CAD><X> </Response></Request>
</CommandRQ>

How do i eliminate the Responsetag?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gertone, since it's a generally a copy-template, it also copies the Version attribute as well, right. So I did not forgot it.
Avatar of tesmc

ASKER

Gertone :
thx for ur help.
aha, @zc2, you are right, I missed the fact that it was already in the source, sorry about that