Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XPath statement to replace values

Posted on 2006-06-28
6
Medium Priority
?
3,616 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
[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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Accepted Solution

by:
mshogren earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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