Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-18
5
Medium Priority
?
362 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 36

Accepted Solution

by:
mccarl earned 1200 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 800 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 36

Expert Comment

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

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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!
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
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 …
Suggested Courses

916 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