Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Excel Generation with XSLT from PL/SQL

Hi
I have a problem with the name of header column in Excel file (xls) generated with XSLT.
For Example:
If I have following query :
select 'month' as '2017-10' from dual;
In the generated Excel I have '2017-10' converted in 0x0032..ecc...
This is the code I use in PL/SQL:
v_context := DBMS_XMLGEN.NEWCONTEXT (v_query); DBMS_XMLGEN.SETNULLHANDLING (v_context, 1); DBMS_XMLGEN.SETCONVERTSPECIALCHARS (v_context, true); v_xml_data := DBMS_XMLGEN.GETXMLTYPE (v_context, DBMS_XMLGEN.NONE); v_output := v_xml_data.transform (XMLTYPE (v_xls_xsl)); v_clob := tool_email_pkg.xmltype2clob (v_output);
The problem is in the XML generated.
My XSL:
<xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > <xsl:template match="/*"> <Worksheet> <xsl:attribute name="ss:Name"> <xsl:value-of select="local-name(/*/*)"/> </xsl:attribute> <Table x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="150"> <Row> <xsl:for-each select="/ROWSET/ROW[1]/*"> <Cell ss:StyleID="StileCella"> <Data ss:Type="String"> <xsl:value-of select="name()"/> </Data> </Cell> </xsl:for-each> </Row> <xsl:apply-templates/> </Table> </Worksheet> </xsl:template><xsl:template match="/*/*"> <Row> <xsl:apply-templates/> </Row></xsl:template><xsl:template match="/*/*/*"> <Cell> <Data ss:Type="String"> <xsl:value-of select="." /> </Data> </Cell></xsl:template></xsl:stylesheet>
Stefano
Best Answer
-
Your problem is with the dbms_xmlgen.getxmltype routine and having a column name starting with a number. XML has specific rules regarding element names, in particular, Element names must start with a letter or underscore, so it won't allow element names beginning with a number. Oracle knows this, so when an attempt is made to convert your query to a row/rowset form, it automatically converts the invalid element name to a valid one.
select dbms_xmlgen.getxmltype(q'[select 'month' as "2017-10" from dualfrom dual
<ROWSET>
<ROW>
<_x0032_017-10>month</_x0032_017-10>
</ROW>
</ROWSET>
Put say an underscore in front...
select dbms_xmlgen.getxmltype(q'[select 'month' as "_2017-10" from dualfrom dual
<ROWSET>
<ROW>
<_2017-10>month</_2017-10>
</ROW>
</ROWSET>
All is fine. You need to understand XML has specific rules and they are there for a good reason.
Answers
-
user10316500 wrote:Hi I have a problem with the name of header column in Excel file (xls) generated with XSLT.For Example:If I have following query :select 'month' as '2017-10' from dual;Stefano
Just as an FYI - the SQL you have is incorrect.
SELECT 'month' AS '2017-10' FROM dual;
ORA-00923: FROM keyword not found where expected00923. 00000 - "FROM keyword not found where expected"*Cause: *Action:Error at Line: 1 Column: 19
You're selecting a "string" and labelling it as a quoted string which without the quotes is an ILLEGAL string (must start with an alpha character, and not contain illegal characters such as the minus sign).
The correct SQL would be :
SELECT 'month' AS x2017_10 FROM dual;
-
Sorry
the correct sql I have is:
- SELECT 'month' AS "2017_10"
- FROM dual;
But the problem remains.
-
user10316500 wrote:But the problem remains.
What problem?? I don't see any Oracle error you've posted.
-
user10316500 wrote:Sorrythe correct sql I have is:SELECT 'month' AS "2017_10" FROM dual; But the problem remains.
use COPY & PASTE so we can see what you do & how Oracle responds.
-
Your problem is with the dbms_xmlgen.getxmltype routine and having a column name starting with a number. XML has specific rules regarding element names, in particular, Element names must start with a letter or underscore, so it won't allow element names beginning with a number. Oracle knows this, so when an attempt is made to convert your query to a row/rowset form, it automatically converts the invalid element name to a valid one.
select dbms_xmlgen.getxmltype(q'[select 'month' as "2017-10" from dualfrom dual
<ROWSET>
<ROW>
<_x0032_017-10>month</_x0032_017-10>
</ROW>
</ROWSET>
Put say an underscore in front...
select dbms_xmlgen.getxmltype(q'[select 'month' as "_2017-10" from dualfrom dual
<ROWSET>
<ROW>
<_2017-10>month</_2017-10>
</ROW>
</ROWSET>
All is fine. You need to understand XML has specific rules and they are there for a good reason.
-
Thanks.
You resolved my problem.
However, after I generate XML I need delete the underscore ( in the excel file I wan't see it).
Stefano
-
Then you'll need to change your XSLT to search and replace any underscore that prefixes node names. However, you can only do this if the node gets turned into text content not a node name in the Excel doc's XML.
-
I used TRANSLATE function and it works:
<Row> <xsl:for-each select="/ROWSET/ROW[1]/*"> <Cell ss:StyleID="StileCella"> <Data ss:Type="String"> <xsl:value-of select="translate(name(), '_', '')"/> </Data> </Cell> </xsl:for-each></Row>
Instead REPLACE function doesn't works.
-
Great. Well if your question is sorted, please mark it as ANSWERED so others with a similar problem will know why and how to solve it.
-
It's noticeable from your posting history that you're a repeat offender in asking questions and never marking them as ANSWERED, this is abusing the forum. If you ask questions and people take the time and effort to help answer them, extend the courtesy and mark answers HELPFUL or ANSWERED if they aided or solved your issue. It not only helps you, it helps maintain the site as a knowledge base for others who may have the same issue and are searching for the solution.
You should go through your posts and close questions by marking with the correct answer.