Solved

XSL - Adding an attribute and renaming a tag?

Posted on 2013-06-24
9
325 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
  • 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now