Solved

XSL: how to copy entire node but replace contents of one given element?

Posted on 2014-02-18
5
351 Views
Last Modified: 2014-02-19
I want to copy over all instances of an xml input but replace the contents (by substring) of one of the elements <Iden> with digital value only.

<Bill>
	<FOP>
		<Iden>R2</Iden>
		<CreditCard>
			<CCCode>DC</CCCode>
			<CCNum>1212301</CCNum>
			<CCExp>
				<Month>2</Month>
				<Year>12</Year>
			</CCExp>
			<Fname>JOHN</Fname>
			<Lname>SMITH</Lname>
		</CreditCard>
	</FOP>
	<Addr Use="T">
		<Iden>R1</Iden>
		<Street>720 W 10 ST</Street>
	</Addr>
</Bill>

Open in new window


will become
<Bill>
	<FOP>
		<Iden>2</Iden>
		<CreditCard>
			<CCCode>DC</CCCode>
			<CCNum>1212301</CCNum>
			<CCExp>
				<Month>2</Month>
				<Year>12</Year>
			</CCExp>
			<Fname>JOHN</Fname>
			<Lname>SMITH</Lname>
		</CreditCard>
	</FOP>
	<Addr Use="T">
		<Iden>1</Iden>
		<Street>720 W 10 ST</Street>
	</Addr>
</Bill>

Open in new window

0
Comment
Question by:badtz7229
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 300 total points
ID: 39869420
Is something like this what you are after...
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="Iden" priority="1">
		<xsl:copy>
			<xsl:value-of select="substring(., 2)" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

Open in new window

Note that it only removes the first character from the <Iden> element's text content. It uses "substring" because you mentioned that in your question. It won't check to see if that first character is a digit/non-digit, or indeed if other characters in the string are non-digits.

If you are 100% sure that 100% of the time you will always have one non-digit followed by one or more digits, then the above will work. Otherwise let me know and list the other possibilities that you might get and we can resolve this in other ways.
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 200 total points
ID: 39869597
A general approach for mainaining digits only would be removing the others with translate

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Iden">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:value-of select="translate(., translate(.,'0123456789', ''), '')" />
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet> 

Open in new window

0
 

Author Closing Comment

by:badtz7229
ID: 39870479
thank you both. i went with @mccarl approach.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39872228
No worries, glad to help!  :)
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change div area and length 1 33
what are list of ebay api errors 1 28
Add shadow behind div 5 24
How to implement an additional command 16 40
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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 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…

792 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