Solved

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

Posted on 2014-02-18
5
353 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 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

Technology Partners: 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

When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
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 define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…

738 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