Subject: Re: [xsl] FW: Problem with namespaces From: JBryant@xxxxxxxxx Date: Thu, 4 Aug 2005 14:27:38 -0500 |
Hi, Curtis, Your issue arises because the XML file has a default namespace: xmlns="urn:schemas-microsoft-com:office:spreadsheet" You need to handle that in your XSL file, thus: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" exclude-result-prefixes="ms o x ss" > <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:template match="ms:Workbook/ms:Worksheet/ms:Table/ms:Row[1]"/> <xsl:template match="ms:Workbook/ms:Worksheet/ms:Table/ms:Row[position() > 1]"> <Inventory> <xsl:for-each select="ms:Cell"> <xsl:choose> <xsl:when test="position() = 1"> <Year><xsl:value-of select="ms:Data"/></Year> </xsl:when> <xsl:when test="position() = 2"> <Make><xsl:value-of select="ms:Data"/></Make> </xsl:when> <xsl:when test="position() = 3"> <Model><xsl:value-of select="ms:Data"/></Model> </xsl:when> </xsl:choose> </xsl:for-each> </Inventory> </xsl:template> </xsl:stylesheet> As you can see, I greatly simplified your file, but I bet you can make sense of it. In addition to the simplifications, the specific changes that matter to this issue are: * I gave the default namespace an identifier (ms). * I used that identifier in all the matches and selects. The output (with Xalan Java 2.4.1, since you need XSLT 1.0) is: <Inventory> <Year>2000</Year> <Make>Buick</Make> <Model>Park Ave</Model> </Inventory> <Inventory> <Year>1998</Year> <Make>Cadillac</Make> <Model>Deville</Model> </Inventory> <Inventory> <Year>1995</Year> <Make>Cadillac</Make> <Model>Deville</Model> </Inventory> (with a little hand indenting for readiblity) Saxon 8.5 produces the same results (indentation aside). Let us know if you need more help. Feel free to use me as a proxy for posting if need be. Jay Bryant Bryant Communication Services (presently consulting at Synergistic Solution Technologies) "Michael Kay" <mike@xxxxxxxxxxxx> 08/04/2005 01:09 PM Please respond to xsl-list@xxxxxxxxxxxxxxxxxxxxxx To <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> cc Subject [xsl] FW: Problem with namespaces -----Original Message----- From: Professional Content LLC [mailto:cdfisher@xxxxxxxxxxxxxxxxxxx] Sent: 04 August 2005 18:14 To: mike@xxxxxxxxxxxx Cc: cdfisher@xxxxxxxxxxxxxx Subject: I have to send this directly... Sorry The big-list server will not accept email in multi-part form (therefore I cannot use my HTML email) and the Gov. facility where I work blocks the server. I have tried other forums but the real XSLT folks hang out at mulberrytec (sigh). Can you post this for me in the list? I would really appreciate it. Thanks, Curtis Problem with namespaces I am converting an Excel generated XML file to another XML format. If I strip out all the namespace declarations pertaining to Microsoft in the xml and xsl files, it works, but if I put them back in, I cannot make this work. I'm stuck with PHP XSLT 1.0 (Sablotron) so anything with 2.0 is not possible. After reading MK's XSLT books and JT's beginning book on namespaces, I feel I have an understanding of why namespaces are needed, and how they should work. But when it comes to how they play on access of nodes with XPath I really don't know why this doesn't work. Here is the XML <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <LastAuthor>Preferred Customer</LastAuthor> <Created>2005-07-28T04:27:20Z</Created> <LastSaved>2005-08-02T16:01:05Z</LastSaved> <Version>10.4219</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <DownloadComponents/> <LocationOfComponents HRef="file:///\\Srcstore\dist1\OfficeXP\Suite8-2002\"/> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>7305</WindowHeight> <WindowWidth>15180</WindowWidth> <WindowTopX>120</WindowTopX> <WindowTopY>105</WindowTopY> <RefModeR1C1/> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s21"> <NumberFormat ss:Format="Currency"/> </Style> <Style ss:ID="s22"> <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/> </Style> <Style ss:ID="s23"> <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/> <NumberFormat ss:Format="Short Date"/> </Style> </Styles> <Names> <NamedRange ss:Name="Category" ss:RefersTo="=inventory!R1C17"/> <NamedRange ss:Name="Year" ss:RefersTo="=inventory!R1C1"/> </Names> <Worksheet ss:Name="inventory"> <Table ss:ExpandedColumnCount="26" ss:ExpandedRowCount="48" x:FullColumns="1" x:FullRows="1"> <Column ss:AutoFitWidth="0" ss:Width="31.5"/> <Column ss:AutoFitWidth="0" ss:Width="75"/> <Column ss:AutoFitWidth="0" ss:Width="123"/> <Column ss:AutoFitWidth="0" ss:Width="93"/> <Column ss:AutoFitWidth="0" ss:Width="71.25"/> <Column ss:Index="7" ss:AutoFitWidth="0" ss:Width="74.25" ss:Span="1"/> <Column ss:Index="9" ss:AutoFitWidth="0" ss:Width="60" ss:Span="2"/> <Column ss:Index="12" ss:AutoFitWidth="0" ss:Width="70.5"/> <Column ss:AutoFitWidth="0" ss:Width="42"/> <Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="58.5"/> <Column ss:AutoFitWidth="0" ss:Width="66.75"/> <Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="91.5"/> <Column ss:AutoFitWidth="0" ss:Width="92.25"/> <Column ss:AutoFitWidth="0" ss:Width="75"/> <Column ss:AutoFitWidth="0" ss:Width="60"/> <Column ss:AutoFitWidth="0" ss:Width="95.25"/> <Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="68.25"/> <Column ss:AutoFitWidth="0" ss:Width="57"/> <Column ss:AutoFitWidth="0" ss:Width="70.5"/> <Column ss:AutoFitWidth="0" ss:Width="71.25"/> <Column ss:AutoFitWidth="0" ss:Width="69.75"/> <Row> <Cell> <Data ss:Type="String">Year</Data> <NamedCell ss:Name="Year"/> </Cell> <Cell> <Data ss:Type="String">Make</Data> </Cell> <Cell> <Data ss:Type="String">Model</Data> </Cell> <Cell> <Data ss:Type="String">TrimLevel</Data> </Cell> <Cell> <Data ss:Type="String">AskingPrice</Data> </Cell> <Cell> <Data ss:Type="String">Mileage</Data> </Cell> <Cell> <Data ss:Type="String">ExteriorColor</Data> </Cell> <Cell> <Data ss:Type="String">InteriorColor</Data> </Cell> <Cell> <Data ss:Type="String">BodyStyle</Data> </Cell> <Cell> <Data ss:Type="String">Doors</Data> </Cell> <Cell> <Data ss:Type="String">Engine</Data> </Cell> <Cell> <Data ss:Type="String">Transmission</Data> </Cell> <Cell> <Data ss:Type="String">Stereo</Data> </Cell> <Cell> <Data ss:Type="String">Stock</Data> </Cell> <Cell> <Data ss:Type="String">VIN</Data> </Cell> <Cell> <Data ss:Type="String">Options</Data> </Cell> <Cell> <Data ss:Type="String">Category</Data> <NamedCell ss:Name="Category"/> </Cell> <Cell> <Data ss:Type="String">GeoRegion</Data> </Cell> <Cell> <Data ss:Type="String">Image</Data> </Cell> <Cell> <Data ss:Type="String">InternetSpecials</Data> </Cell> <Cell> <Data ss:Type="String">Date</Data> </Cell> <Cell> <Data ss:Type="String">Color</Data> </Cell> <Cell> <Data ss:Type="String">CarCost</Data> </Cell> <Cell> <Data ss:Type="String">AdditionalExpenses</Data> </Cell> <Cell> <Data ss:Type="String">TotalCost </Data> </Cell> <Cell> <Data ss:Type="String">Notes</Data> </Cell> </Row> <Row> <Cell> <Data ss:Type="Number">2000</Data> </Cell> <Cell> <Data ss:Type="String">Buick</Data> </Cell> <Cell> <Data ss:Type="String">Park Ave </Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">8995</Data> </Cell> <Cell> <Data ss:Type="Number">15663</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">Four Door</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="Number">14</Data> </Cell> <Cell> <Data ss:Type="Number">1125421</Data> </Cell> <Cell ss:StyleID="Default"> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="String">Domestic Autos</Data> </Cell> <Cell> <Data ss:Type="String">USA</Data> </Cell> <Cell ss:Formula="=RC[-5]&".jpg""> <Data ss:Type="String">14.jpg</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s23"> <Data ss:Type="DateTime">2005-02-18T00:00:00.000</Data> </Cell> <Cell> <Data ss:Type="String">White</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">4900</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">422.64</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">5322.64</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> </Row> <Row> <Cell> <Data ss:Type="Number">1998</Data> </Cell> <Cell> <Data ss:Type="String">Cadillac</Data> </Cell> <Cell> <Data ss:Type="String">Deville</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">8995</Data> </Cell> <Cell> <Data ss:Type="Number">768942</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">Four Door</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="Number">8</Data> </Cell> <Cell> <Data ss:Type="Number">99958</Data> </Cell> <Cell ss:StyleID="Default"> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="String">Domestic Autos</Data> </Cell> <Cell> <Data ss:Type="String">USA</Data> </Cell> <Cell ss:Formula="=RC[-5]&".jpg""> <Data ss:Type="String">8.jpg</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s23"> <Data ss:Type="DateTime">2005-03-04T00:00:00.000</Data> </Cell> <Cell> <Data ss:Type="String">Pearl</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">5135</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">1534.07</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">6669.07</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> </Row> <Row> <Cell> <Data ss:Type="Number">1995</Data> </Cell> <Cell> <Data ss:Type="String">Cadillac</Data> </Cell> <Cell> <Data ss:Type="String">Deville</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">3195</Data> </Cell> <Cell> <Data ss:Type="Number">302539</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">Four Door</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell> <Data ss:Type="Number">2</Data> </Cell> <Cell> <Data ss:Type="Number">156343</Data> </Cell> <Cell ss:StyleID="Default"> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="String">Domestic Autos</Data> </Cell> <Cell> <Data ss:Type="String">USA</Data> </Cell> <Cell ss:Formula="=RC[-5]&".jpg""> <Data ss:Type="String">2.jpg</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> <Cell ss:StyleID="s23"> <Data ss:Type="DateTime">2005-05-07T00:00:00.000</Data> </Cell> <Cell> <Data ss:Type="String">Gray</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">800</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">100</Data> </Cell> <Cell ss:StyleID="s21"> <Data ss:Type="Number">900</Data> </Cell> <Cell> <Data ss:Type="String">None</Data> </Cell> </Row> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <Selected/> <FreezePanes/> <SplitHorizontal>1</SplitHorizontal> <TopRowBottomPane>23</TopRowBottomPane> <ActivePane>2</ActivePane> <Panes> <Pane> <Number>3</Number> </Pane> <Pane> <Number>2</Number> <ActiveRow>1</ActiveRow> <ActiveCol>25</ActiveCol> <RangeSelection>R2C26:R48C26</RangeSelection> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> <Sorting xmlns="urn:schemas-microsoft-com:office:excel"> <Sort>Category</Sort> <Sort>Make</Sort> <Sort>Year</Sort> </Sorting> </Worksheet> </Workbook> Here is the XSL <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:template name="rows" match="/" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > <xsl:element name="Inventory"> <xsl:for-each select="Workbook/Worksheet/Table/Row"> <xsl:if test="position() > 1"> <xsl:for-each select="Cell"> <xsl:choose> <xsl:when test="position() = 1"> <xsl:element name="Year"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 2"> <xsl:element name="Make"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 3"> <xsl:element name="Model"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 4"> <xsl:element name="TrimLevel"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 5"> <xsl:element name="AskingPrice"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 6"> <xsl:element name="Mileage"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 7"> <xsl:element name="ExteriorColor"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 8"> <xsl:element name="InteriorColor"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 9"> <xsl:element name="BodyStyle"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 10"> <xsl:element name="Doors"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 11"> <xsl:element name="Engine"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 12"> <xsl:element name="Transmission"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 13"> <xsl:element name="Stereo"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 14"> <xsl:element name="Stock"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 15"> <xsl:element name="VIN"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 16"> <xsl:element name="Options"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 17"> <xsl:element name="Category"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 18"> <xsl:element name="GeoRegion"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 19"> <xsl:element name="Image"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 20"> <xsl:element name="InternetSpecials"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 21"> <xsl:element name="Date"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 22"> <xsl:element name="Color"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 23"> <xsl:element name="CarCost"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 24"> <xsl:element name="AdditionalExpenses"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 25"> <xsl:element name="TotalCost"><xsl:value-of select="Data"/></xsl:element> </xsl:when> <xsl:when test="position() = 26"> <xsl:element name="Notes"><xsl:value-of select="Data"/></xsl:element> </xsl:when> </xsl:choose> <!--<xsl:call-template name="getUnit"/> <xsl:element name="node" ><xsl:value-of select="Data"/></xsl:element> <xsl:value-of select="$elementTree/ss:node[position()]//*/name()"/> <xsl:for-each select="./ss:Data"> <xsl:for-each select="$elementTree/ss:node"> <xsl:value-of select="$elementTree/node[position()]/name()"></xsl:value-of> </xsl:for-each> </xsl:for-each> </xsl:element>--> </xsl:for-each> </xsl:if> </xsl:for-each> </xsl:element> <xsl:apply-templates /> </xsl:template> </xsl:stylesheet> As I said, this works fine with the office namespaces removed. But since I'm looking for an automated solution, it's not an option. Thanks, and I appreciate the expert advice only available in this list. Curtis
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] FW: Problem with namespac, Chenzhou Cui | Thread | Re: [xsl] FW: Problem with namespac, Jay Bryant |
RE: [xsl] Using Saxon 8.5 and colle, Michael Kay | Date | Re: [xsl] Using Saxon 8.5 and colle, Colin Paul Adams |
Month |