Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2007, 02:47 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I get rid of xmlns= attributes?

I am an absolute novice at this; in fact, this is the first time I've worked with XML/XST Transforms at all. I'm very lost, and in dire need of assistance. Here's the problem:

We receive several XML files from a vendor on a daily basis. We've been using a VB application to parse and import the data from these files into our SQL database, but now we are re-engineering everything into BizTalk, but the BizTalk process that's been built cannot read the source files because all the data is encoded as attributes in a "row" node, rather than as individual nodes themselves (which is what the BizTalk process requires).

Here's a sample of the source file:

<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
- <s:Schema id="RowsetSchema">
- <s:ElementType name="row" content="eltOnly">
- <s:AttributeType name="NDC" rs:number="1" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="11" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_SOURCE" rs:number="2" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="6" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_TYPE" rs:number="3" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_EFF_DATE_1" rs:number="4" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_AMT_1" rs:number="5" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_EFF_DATE_2" rs:number="6" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_AMT_2" rs:number="7" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_EFF_DATE_3" rs:number="8" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PRICE_AMT_3" rs:number="9" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="DATE_TIME_CREATED" rs:number="10" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="dateTime" rs:dbtype="timestamp" dt:maxLength="16" rs:scale="0" rs:precision="19" rs:fixedlength="true" />
  </s:AttributeType>
- <s:AttributeType name="USER_ID" rs:number="11" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" />
  </s:AttributeType>
- <s:AttributeType name="DATE_TIME_MODIFIED" rs:number="12" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="dateTime" rs:dbtype="timestamp" dt:maxLength="16" rs:scale="0" rs:precision="19" rs:fixedlength="true" />
  </s:AttributeType>
- <s:AttributeType name="FORM_ID" rs:number="13" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="10" />
  </s:AttributeType>
- <s:AttributeType name="PACKAGE_PRICE_1" rs:number="14" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PACKAGE_PRICE_2" rs:number="15" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
- <s:AttributeType name="PACKAGE_PRICE_3" rs:number="16" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="number" rs:dbtype="varnumeric" dt:maxLength="20" rs:scale="0" rs:precision="38" />
  </s:AttributeType>
  <s:extends type="rs:rowbase" />
  </s:ElementType>
  </s:Schema>
- <rs:data>
  <z:row NDC="00009738601" PRICE_SOURCE="FDB" PRICE_TYPE="FFP" PRICE_EFF_DATE_1="20020122" PRICE_AMT_1=".0573" PRICE_EFF_DATE_2="20001207" PRICE_AMT_2=".074" PRICE_EFF_DATE_3="19971001" PRICE_AMT_3=".0399" DATE_TIME_CREATED="2003-11-25T07:40:41" USER_ID="PHIDBA" DATE_TIME_MODIFIED="2003-11-25T07:40:41" FORM_ID="BATCH" PACKAGE_PRICE_1="0" PACKAGE_PRICE_2="0" PACKAGE_PRICE_3="0" />


So, like anyone starting out with a new language, I searched the 'net for a way to do this and found this article (http://www.itjungle.com/mgo/mgo101003-story01.html) that seemed to cover it, by employing the following XSLT:

  <xsl:output indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="*">
    <xsl:copy>
      <xsl:if test="@*">
        <xsl:for-each select="@*">
          <xsl:element name="{name()}">
            <xsl:value-of select="." />
          </xsl:element>
        </xsl:for-each>
      </xsl:if>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


So I wrote a simple C# program (my first) to apply the transform to the data thusly:

            string InputFileName = @"C:\RXA_XML\DP070625.xml";
            string OutputFileName = @"C:\RXA_XML\MyTestFile.xml";
            DataSet ds = new DataSet();
            System.IO.FileStream myFileStream = new System.IO.FileStream
                (OutputFileName, System.IO.FileMode.Create);

            try
            {
                ds.ReadXml(InputFileName);

                XmlDataDocument xmlDoc = new XmlDataDocument(ds);
                XslTransform xslTran = new XslTransform();
                xslTran.Load(@"C:\RXA_XML\transform.xsl");
                XmlTextWriter writer = new XmlTextWriter(@"C:\RXA_XML\xslt_output.xml",System .Text.Encoding.UTF8);
                xslTran.Transform(xmlDoc, null, writer);
                writer.Close();

                        catch...(etc.)


It seemed to work just fine, except I am getting "xmlns=" namespace declarations in all the nodes, which again, BizTalk can't seem to cope with:

<xml>
- <data xmlns="urn:schemas-microsoft-com:rowset">
- <row xmlns="#RowsetSchema">
  <NDC xmlns="">00009738601</NDC>
  <PRICE_SOURCE xmlns="">FDB</PRICE_SOURCE>
  <PRICE_TYPE xmlns="">FFP</PRICE_TYPE>
  <PRICE_EFF_DATE_1 xmlns="">20020122</PRICE_EFF_DATE_1>
  <PRICE_AMT_1 xmlns="">.0573</PRICE_AMT_1>
  <PRICE_EFF_DATE_2 xmlns="">20001207</PRICE_EFF_DATE_2>
  <PRICE_AMT_2 xmlns="">.074</PRICE_AMT_2>
  <PRICE_EFF_DATE_3 xmlns="">19971001</PRICE_EFF_DATE_3>
  <PRICE_AMT_3 xmlns="">.0399</PRICE_AMT_3>
  <DATE_TIME_CREATED xmlns="">2003-11-25T07:40:41</DATE_TIME_CREATED>
  <USER_ID xmlns="">PHIDBA</USER_ID>
  <DATE_TIME_MODIFIED xmlns="">2003-11-25T07:40:41</DATE_TIME_MODIFIED>
  <FORM_ID xmlns="">BATCH</FORM_ID>
  <PACKAGE_PRICE_1 xmlns="">0</PACKAGE_PRICE_1>
  <PACKAGE_PRICE_2 xmlns="">0</PACKAGE_PRICE_2>
  <PACKAGE_PRICE_3 xmlns="">0</PACKAGE_PRICE_3>
  </row>



Can anyone please tell me how to eliminate all the namespace declarations from the output file? I've tried using exclude-result-prefixes, and copy-namespaces="no" to no avail (in fact, the latter bombs, telling me it's not a recognized command).

Thanks in advance.

pete


 
Old July 5th, 2007, 08:06 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

hi,

See this thread. I had the same problem:

http://p2p.wrox.com/topic.asp?TOPIC_ID=60007
 
Old July 6th, 2007, 07:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't worry about the namespace declarations, worry about the names of the elements. (The name has two parts, a namespace URI and a local name). If you generate elements with the right names, the XSLT processor will produce the right namespace declarations.

I don't know what you actually want to produce. If you want to lose all the namespace information, don't use xsl:copy. xsl:copy will never change the name of an element; if you want to change its name, use xsl:element. For example <xsl:element name="{local-name()}"> will create a new element that has the same local name as the current element, but which is in no namespace.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2007, 05:10 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thank you for the help. I went back and looked at the transform, and noticed it's already using <xsl:element name="{local-name()}"> inside the for-each loop. Where would changing the copy come in?

I really do apologize. This is the first time I've had any exposure at all to XSL programming and working with XML files.

Thanks,
pete

 
Old July 10th, 2007, 05:18 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW, this is what I need the output to look like, with each row, formerly made of attributes, now being well-formed:

<xml>
  <data>
    <row>
      <NDC>00009738601</NDC>
      <PRICE_SOURCE>FDB</PRICE_SOURCE>
      <PRICE_TYPE>FFP</PRICE_TYPE>
      <PRICE_EFF_DATE_1>20020122</PRICE_EFF_DATE_1>
      <PRICE_AMT_1>.0573</PRICE_AMT_1>
      <PRICE_EFF_DATE_2>20001207</PRICE_EFF_DATE_2>
      <PRICE_AMT_2>.074</PRICE_AMT_2>
      <PRICE_EFF_DATE_3>19971001</PRICE_EFF_DATE_3>
      <PRICE_AMT_3>.0399</PRICE_AMT_3>
      <DATE_TIME_CREATED>2003-11-25T07:40:41</DATE_TIME_CREATED>
      <USER_ID>PHIDBA</USER_ID>
      <DATE_TIME_MODIFIED>2003-11-25T07:40:41</DATE_TIME_MODIFIED>
      <FORM_ID>BATCH</FORM_ID>
      <PACKAGE_PRICE_1>0</PACKAGE_PRICE_1>
      <PACKAGE_PRICE_2>0</PACKAGE_PRICE_2>
      <PACKAGE_PRICE_3>0</PACKAGE_PRICE_3>
    </row>
  </data>
</xml>

Again, my thanks in advance.

pete

 
Old July 10th, 2007, 05:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You are generating some elements using xsl:copy and some using xsl:element. The elements created using xsl:copy are retaining their existing names, which are in a namespace, which apparently you don't want. So you can't use xsl:copy.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2007, 03:33 PM
Registered User
 
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thank you for all your help! Using the direction you gave, I finally figured out what needed to be changed. Here's what I wound up using that worked:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*" />
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:if test="@*">
        <xsl:for-each select="@*">
          <xsl:element name="{name()}">
            <xsl:value-of select="." />
          </xsl:element>
        </xsl:for-each>
      </xsl:if>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Thank you again! You have no idea how much your help meant!

pete






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to match attributes xmlns:tns etc MrWay XSLT 2 February 25th, 2007 10:38 AM
XMLNS mathias XML 0 February 20th, 2007 04:46 AM
xmlns albusr .NET Web Services 0 December 24th, 2005 08:25 AM
how do i get rid of table attributes in datagrids whyulil ASP.NET 1.0 and 1.1 Basics 2 September 8th, 2004 01:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.