Solved

XSL - Adding an attribute and renaming a tag?

Posted on 2013-06-24
9
333 Views
Last Modified: 2013-06-24
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> ?
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 39272652
You will need a template for

<xsl:template match="CommandRS">
    <CommandRQ Version="2003.XML1.0.1">
      <xsl:apply-templates/>
    </CommandRQ>
</xsl:template>

and you need to replace the xsl:copy in the template for Response with the new name literal element


<xsl:template match="Response">
    <Request>
...
</Request>
0
 
LVL 18

Expert Comment

by:zc2
ID: 39272664
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

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39272696
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
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:badtz7229
ID: 39272745
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?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39272750
drop the xsl:copy tag

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

Open in new window

0
 
LVL 18

Expert Comment

by:zc2
ID: 39272757
Gertone, since it's a generally a copy-template, it also copies the Version attribute as well, right. So I did not forgot it.
0
 

Author Closing Comment

by:badtz7229
ID: 39272760
Gertone :
thx for ur help.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39272766
aha, @zc2, you are right, I missed the fact that it was already in the source, sorry about that
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39272768
welcome
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question