Root Namespace & Finding/Updating Elements w/XPath

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
c#, XSLT transform, asp.net, Accord 2.24.00.

<span style="text-decoration:underline History & Some Details:
I have a working XSL that early on I abandoned working with the root namespaces, hoping that as I learned XSLT transforming Id figure that out, well not so much...

In my initial development I used Linq to write all the transformations, additions and updates, I got through about 10% of them and realized that I preferred to XSLT, so I abondoned Linq. I now have an XSL that contains nested templates with sub templates, variables for
the IDs needed for the relation elements that is based on a random number generator, I have lookup tables for the typeCode attributes, etc, etc... All of these are for the Accord TXLife standard.

--- Using Linq I found that when the namespaces were present I needed to write my updates/additions with a namespace manager, and that was working well enough before I abandoned Linq as noted above.

-- I now have to deal with the root namespaces and other features.

- There is an OrderDate from the CRM system that I also populate the transaction date & time fields/elements as defaults. Then on the actual send Ill update those with the TransExeDate & TransExeTime, that too is working when the root namespaces
are NOT present.

- Also, if for some reason the Paramed Order is Re-Sent Im updating the transaction date & time once more.

--------------------------------------------------------
Q1: Challenge to populate the namespace as noted below.

Q2. How do I make updates / find the elements (see code snipit) using XmlDocument FindSingleNode & ReplaceChild, or better method?
--------------------------------------------------------

XSL Root with problematic namespaces (current dev state)

<div style="background-color:white; color:black
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black " <span style="color:red encoding<span style="color:blue =<span style="color:black "<span style="color:blue iso-8859-1<span style="color:black "<span style="color:blue ?>

<xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0"

xmlns:msxml="urn:schemas-microsoft-com:xslt"

xmlns:random="http://www.microsoft.com/msxsl"

exclude-result-prefixes="msxml random"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns="http://ACORD.org/Standards/Life/2"

xsi:schemaLocation="http://ACORD.org/Standards/Life/2 TXLife2.24.00enum.xsd"

>



[/code]




XML (current dev state)


<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 TXLife

<span style="color:red xmlns<span style="color:blue =<span style="color:black "<span style="color:blue http://ACORD.org/Standards/Life/2<span style="color:black "

<span style="color:red xmlns:xsi<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema-instance<span style="color:black "

<span style="color:red xmlns:xsd<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema<span style="color:black "

<span style="color:blue >



[/code]



XML (desired state)

<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 TXLife

<span style="color:red xmlns:xsi<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema-instance<span style="color:black "

<span style="color:red xmlns:xsd<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema<span style="color:black "

<span style="color:red xmlns<span style="color:blue =<span style="color:black "<span style="color:blue http://ACORD.org/Standards/Life/2<span style="color:black "

<span style="color:red xsi:schemaLocation<span style="color:blue =<span style="color:black "<span style="color:blue http://ACORD.org/Standards/Life/2 TXLife2.24.00enum.xsd<span style="color:black "

<span style="color:blue >



[/code]




Transaction Date Time... With this as the XSL root...


<div style="background-color:white; color:black
<pre><span style="color:blue <?<span style="color:#a31515 xml <span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black " <span style="color:red encoding<span style="color:blue =<span style="color:black "<span style="color:blue iso-8859-1<span style="color:black "<span style="color:blue ?>

<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 stylesheet

<span style="color:red xmlns:xsl<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/1999/XSL/Transform<span style="color:black "

<span style="color:red version<span style="color:blue =<span style="color:black "<span style="color:blue 1.0<span style="color:black "

<span style="color:red xmlns:msxml<span style="color:blue =<span style="color:black "<span style="color:blue urn:schemas-microsoft-com:xslt<span style="color:black "

<span style="color:red xmlns:random<span style="color:blue =<span style="color:black "<span style="color:blue http://www.microsoft.com/msxsl<span style="color:black "

<span style="color:red exclude-result-prefixes<span style="color:blue =<span style="color:black "<span style="color:blue msxml random<span style="color:black "

<span style="color:blue >



[/code]



Im able to populate & make updates w/out the problematic namespaces

<div style="background-color:white; color:black
<pre> <span style="color:blue <<span style="color:#a31515 TransExeDate<span style="color:blue >2011-03-16<span style="color:blue </<span style="color:#a31515 TransExeDate<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 TransExeTime<span style="color:blue >02:46:32-0897<span style="color:blue </<span style="color:#a31515 TransExeTime<span style="color:blue >



[/code]



With the problematic namespaces I get the xmlns artifacts, even if I try to remove, which I think only removes the content between the = "", not the xmlns itself, see snipit below.

<div style="background-color:white; color:black
<pre> <TransExeDate xmlns=""<span style="color:blue </<span style="color:#a31515 span<span style="color:blue >>2011-03-16<span style="color:blue </<span style="color:#a31515 TransExeDate<span style="color:blue >

<TransExeTime xmlns=""<span style="color:blue </<span style="color:#a31515 span<span style="color:blue >>02:46:55-4655<span style="color:blue </<span style="color:#a31515 TransExeTime<span style="color:blue >



[/code]



XSL TXLife Root template section...

<div style="background-color:white; color:black
<pre> <span style="color:green <!-- Wrapper top level TXLife-->

<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template <span style="color:red match<span style="color:blue =<span style="color:black "<span style="color:blue /<span style="color:black "<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 TXLife<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 UserAuthRequest<span style="color:blue >

<span style="color:green <!--hardcoded during dev-->

<span style="color:blue <<span style="color:#a31515 UserLoginName<span style="color:blue >zebraZooYou<span style="color:blue </<span style="color:#a31515 UserLoginName<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 UserPswd<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 CryptType<span style="color:blue >NONE<span style="color:blue </<span style="color:#a31515 CryptType<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 Pswd<span style="color:blue >qwerty<span style="color:blue </<span style="color:#a31515 Pswd<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 UserPswd<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 VendorApp<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 AppName<span style="color:blue >CompApit Automated Paramed Orders<span style="color:blue </<span style="color:#a31515 AppName<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 AppVer<span style="color:blue >0.1.0<span style="color:blue </<span style="color:#a31515 AppVer<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 VendorApp<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 UserAuthRequest<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 TXLifeRequest<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 call-template <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue txLifeReq_Header<span style="color:black " <span style="color:blue />

<span style="color:blue <<span style="color:#a31515 OLife<span style="color:blue >

<span style="color:blue <<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 call-template <span style="color:red name<span style="color:blue =<span style="color:black "<span style="color:blue OLife<span style="color:black "<span style="color:blue />

<span style="color:blue </<span style="color:#a31515 OLife<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 TXLifeRequest<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 TXLife<span style="color:blue >

<span style="color:blue </<span style="color:#a31515 xsl<span style="color:blue :<span style="color:#a31515 template<span style="color:blue >



[/code]



Here is my snipit when the problematic namespaces are in, which Im able to make the update, but get the xmlns artifact.

This routine is passed the XmlDocument in the initial transform & file system save and also from the re-send user action.
If an error is returned the original document is returned.

Omitting the namespace manager and using */TransExeDate & Time XPath works when the problematic namespaces are not present. Here too Im using two different ways to try to remove the xmlns, first in TransExeDate RemoveNamedItem, and in TransExeTime,
NamespaceURI.Remove(), both fail to remove the xmlns.

<div style="background-color:white; color:black
<pre><span style="color:blue protected XmlDocument addTransDateTime(XmlDocument x)

{

XmlDocument xOld = <span style="color:blue new XmlDocument();

xOld = x;

<span style="color:blue string xnTransDateNew = <span style="color:#a31515 "descendant::SMM:TransExeDate";

<span style="color:blue string xnTransTimeNew = <span style="color:#a31515 "descendant::SMM:TransExeTime";

<span style="color:blue string xnTransDateFix = <span style="color:#a31515 "descendant::TransExeDate";

<span style="color:blue string xnTransTimeFix = <span style="color:#a31515 "descendant::TransExeTime";

<span style="color:blue string sDate = <span style="color:blue string.Format(<span style="color:#a31515 "{0:yyyy-MM-dd}", DateTime.Now);

<span style="color:blue string sTime = <span style="color:blue string.Format(<span style="color:#a31515 "{0:HH:mm:ss-ms}", DateTime.Now);

<span style="color:blue try

{

XmlElement root = x.DocumentElement;

XmlNamespaceManager nsm = <span style="color:blue new XmlNamespaceManager(x.NameTable);

nsm.AddNamespace(<span style="color:#a31515 "SMM", <span style="color:#a31515 "http://ACORD.org/Standards/Life/2");



XmlNode nTXLifeRequest = root.SelectSingleNode(xnTransDateNew, nsm);

XmlNode nDate = x.CreateElement(<span style="color:#a31515 "TransExeDate");

nDate.InnerText = sDate;

nTXLifeRequest.ParentNode.ReplaceChild(nDate, nTXLifeRequest);

nTXLifeRequest = root.SelectSingleNode(xnTransDatFix);

nTXLifeRequest.Attributes.RemoveNamedItem(<span style="color:#a31515 "xmlns");



nTXLifeRequest = root.SelectSingleNode(xnTransTimeNew, nsm);

XmlNode nTime = x.CreateElement(<span style="color:#a31515 "TransExeTime");

nTime.InnerText = sTime;

nTXLifeRequest.ParentNode.ReplaceChild(nTime, nTXLifeRequest);

nTXLifeRequest = root.SelectSingleNode(xnTransTimeFix);

nTXLifeRequest.NamespaceURI.Remove(0);



<span style="color:blue return x;

}

<span style="color:blue catch (Exception ex)

{

lblStatus.Text = <span style="color:#a31515 "Error: msXmlTransform - " + ex.Message.ToString();

lblStatus.Visible = <span style="color:blue true;

<span style="color:blue return xOld;

}

}

[/code]

TIA





<
JeffP...

View the full article
 
Back
Top