• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3628
  • Last Modified:

XPath statement to replace values

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
k3n51mm
Asked:
k3n51mm
  • 3
  • 2
1 Solution
 
jkmyoungCommented:
Are you looking for something like:
xPath="//anType[contains(., Conformance)]"
or xPath="//*[contains(., Conformance)]"

?
0
 
k3n51mmAuthor Commented:
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
 
mshogrenCommented:
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.

 
mshogrenCommented:
Here is an example of replacing strings in XSLT

http://www.xml.com/pub/a/2002/06/05/transforming.html
0
 
k3n51mmAuthor Commented:
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
 
mshogrenCommented:
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

Industry Leaders: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now