I am trying something very simple, but for some reason this is not working. Basically, I need to rename some nodes in the XML document. So I created an XSLT file for conversion.
Here is an example XML:
EDIT: Addresses and address elements are found at many levels. That's why I had to try applying XSLT. The Visual Studio dataset function, which creates typed datasets from XSD files, does not allow you to have nested references to the same table. Thus, the presence of Business / Business / Addresses and Business / Business / Contact / Addresses causes Load () to fail. This is a known issue, and all they tell you is something like "Don't have nested table links ... edit your XSD to stop this." Unfortunately, this means that we must use XSLT so that the XML matches the cracked XSD, as the files come from a third-party provider.
So, we are very close with the help provided here. The last two things:
1.) How do I use the namespace link in the xsl: template matching attribute to indicate that I want to rename business / business / addresses to BusinessAddresses, but rename Business / Business / Contacts / Contacts / Addresses to ContactAddresses?
2.) How can I stop XSLT from cluttering up each new item with explicit namespace references? This causes a strong inflation in the outlet.
I created a namespace called steel, and I had a good success:
<xsl:template match="steel:Addresses> <xsl:element name="BusinessAddresses> </xsl:template>
The obvious problem is that it renames ALL Address elements in BusinessAddresses, although I want some of them to be called ContactAddresses, etc. Adding useless explicit namespace references to all renamed nodes also causes problems.
I tried things like this, but as soon as I add slashes to the match attribute, this is a syntax error in XSLT, for example:
<xsl:template match="steel:/Businesses/Business/Addresses">
I feel very close, but you need to be guided by how to mix the use of namespace and the way to use a slash to select ANY nodes in certain ways.
<?xml version="1.0"?> <Businesses> <Business> <BusinessName>Steel Stretching</BusinessName> <Addresses> <Address> <City>Pittsburgh</City> </Address> <Address> <City>Philadelphia</City> </Address> </Addresses> <Contacts> <Contact> <FirstName>Paul</FirstName> <LastName>Jones</LastName> <Addresses> <Address> <City>Pittsburgh</City> </Address> </Addresses> </Contact> </Contacts> </Business> <Business> <BusinessName>Iron Works</BusinessName> <Addresses> <Address> <City>Harrisburg</City> </Address> <Address> <City>Lancaster</City> </Address> </Addresses> </Business> </Businesses>
I need to rename Addresses to BusinessAddresses, and I need to rename Address to BusinessAddress, for each instance of Addresses and Address directly to Business node. I also need to rename the Addresses in ContactAddresses, and I need to rename the address in ContactAddress for each instance of Addresses and addresses directly in the contact node.
I tried several solutions, but no one works. All of them produce the same XML as the source file.
Here is an example of what I tried:
<xsl:template match="/"> <xsl:apply-templates select="@*|node()" /> </xsl:template> <xsl:template match="@*|*"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="Addresses"> <BusinessAddresses> <xsl:apply-templates select="@*|node()" /> </BusinessAddresses> </xsl:template>
This has been tested in at least 6 different variations, including the XSLT step-by-step debugger in VB.Net. It never performs pattern matching for addresses.
Why?