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

x
?
Solved

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

Posted on 2014-02-18
5
Medium Priority
?
358 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
[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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
Suggested Courses

722 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