tesmc
asked on
XSL - Adding an attribute and renaming a tag?
I have the following input
<CommandRS Version="2003.XML1.0.1">
<Response>MR - COLLECT<X> X IF SAME FOP OR OVERRIDE <USD></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, '<')">
<xsl:text disable-output-escaping="y es"><</ xsl:text>
<xsl:value-of select="substring-before(s ubstring-a fter($str, '<'), '>')"></xsl:value-of>
<xsl:text disable-output-escaping="y es">></ xsl:text>
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="substring-after($s tr, '>')"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="not (contains($str, '<'))">
<xsl:text disable-output-escaping="y es"> <X> </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> ?
<CommandRS Version="2003.XML1.0.1">
<Response>MR - COLLECT<X> X IF SAME FOP OR OVERRIDE <USD></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, '<')">
<xsl:text disable-output-escaping="y
<xsl:value-of select="substring-before(s
<xsl:text disable-output-escaping="y
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="substring-after($s
</xsl:call-template>
</xsl:if>
<xsl:if test="not (contains($str, '<'))">
<xsl:text disable-output-escaping="y
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Gertone, since it's a generally a copy-template, it also copies the Version attribute as well, right. So I did not forgot it.
ASKER
Gertone :
thx for ur help.
thx for ur help.
aha, @zc2, you are right, I missed the fact that it was already in the source, sorry about that
welcome
Open in new window