Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSL - Adding an attribute and renaming a tag?

Posted on 2013-06-24
9
Medium Priority
?
335 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 2000 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 19

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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 19

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

704 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