Solved

XPath statement to replace values

Posted on 2006-06-28
6
3,565 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tag a picture in Word 2013? 6 46
selectSingleNode in Access 2013 11 53
VB.Net and XML XSD Files 1 53
MS Access XML Export Query Setup Multiple Tag Values 15 28
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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