Solved

XSL - Adding an attribute and renaming a tag?

Posted on 2013-06-24
9
331 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Independent Software Vendors: 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

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

749 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