Solved

XPath statement to replace values

Posted on 2006-06-28
6
3,558 Views
Last Modified: 2012-06-21
Platform: Windows, VB.Net 2003

I have to process an incoming XML file by replacing certain element contents with other strings. For example:

incoming data:             <anType>Conformance_1</anType>
needs to change to:     <anType>Conf1</anType>

This is a simple string replacement operation, but it's a kludge from hell to change the entire XML file to a string variable to replace these substrings.

But, we have around 100 of these valid values that we must change in the XML doc. Each valid value may occur multiple times. I wanted to use an embedded stylesheet but VS2003 does not support  XslCompiledTransform() like VS2005 does.

So now I am trying the XPathNavigator. What I need is an XPath statement I can use to 1) select all the elements in the XML document that contain the target string, and then 2) replace each occurrence with the correct replacement string.

Whatever solution I get, I will need to implement around 100 times.

THANKS
0
Comment
Question by:k3n51mm
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:jkmyoung
ID: 17004350
Are you looking for something like:
xPath="//anType[contains(., Conformance)]"
or xPath="//*[contains(., Conformance)]"

?
0
 
LVL 1

Author Comment

by:k3n51mm
ID: 17004463
I am looking for something that does what I described. Therefore if either of your lines of code performs a selction and then enables replacement, then fine.

I have the feeling you implied an unspoken question there that assumes that I know more than I do about XPath statements.

thx
0
 
LVL 15

Expert Comment

by:mshogren
ID: 17005511
XPath statements do not replace values by themselves.  You will need to use XSLT or the simple text methods which you are averse to.  Here is an example of doing the transformation in VB.NET

http://msdn2.microsoft.com/en-us/library/5b9f8zhh.aspx

You will need to supply a different XSL stylesheet which does an identity transformation on all elements except for those which are selected by jkmyoung's statements above.  I will try to help with that shortly.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 15

Accepted Solution

by:
mshogren earned 500 total points
ID: 17005609
Here is an example of replacing strings in XSLT

http://www.xml.com/pub/a/2002/06/05/transforming.html
0
 
LVL 1

Author Comment

by:k3n51mm
ID: 17012914
hi mshogren, thanks very much for your response. After lots of trial and error and using the links you sent, I'm getting close to what I need. I am only left with a couple of problems, I think in the <xsl:output> node.

When I transform the incoming xml file, I'm currently just writing it to disk for debugging so I can see the output from the xsl. It's close to being correct, and my global replacement is working on the one value I've tested with. So, that part is okay. However, for some reason my transformed output is not getting either a root node declaration or a DOCTYPE declaration. And, i'm losing my indentation even though I'm adding the attribute in the xsl:output statement.

I know neither of these are actually within the XML nodes themselves, but I'm wondering how to include them in the transformed output.

For example, the incoming xml file has these two lines to begin with:
       
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Header SYSTEM "GENERAL_1_1.dtd">

....but they are not making it into the output, and they're required obviously. I can't perform any more processing on an xml document with no root node. Also, the DTD reference must be included in the output as well, and I don't know how to simply copy it to the output.

The last issue is that I'm losing indentation even though I'm using this in the xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template name="globalReplace">  (.... do stuff here, everything is ok)

Any input you can share is greatly appreciated.
0
 
LVL 15

Expert Comment

by:mshogren
ID: 17013252
I am not sure why the indenting isn't working for you.  I haven't tried this myself but your problems may be solved by using the omit-xml-declaration and doctype-system attributes of the xsl:output tag.  If the doc type is always the same then you should just be able to put it in the stylesheet rather than trying to copy it.

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no" doctype-system="GENERAL_1_1.dtd" />

Let me know how that goes.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now