Jump to content

Element With "xmlns" Attribute Not Added To Selection


Recommended Posts

Hi, this is Giovanni, I've started few days ago reading an XML book, to upskill on this fast growing adoption language.I was doing my sample exercises, really for beginners, when I've found a problem, that I was not able to solve. I have this answer from a SAP call: *****************************<XML>************************************<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="SapCompanyCode.xsl"?> <BAPI_COMPANYCODE_GETLIST.Response xmlns="urn:sap-com:document:sap:rfc:functions"> <RETURN xmlns=""> <LOG_MSG_NO>000000</LOG_MSG_NO> </RETURN> <COMPANYCODE_LIST xmlns=""> <item> <COMP_CODE>0001</COMP_CODE> <COMP_NAME>SAP China</COMP_NAME> </item> <item> <COMP_CODE>0MB1</COMP_CODE> <COMP_NAME>IS-B Musterbank Deutschl.</COMP_NAME> </item>........ ******************************</XML>************************************ I started building my xsl, these are the more important lines: *******************************<XSL>***********************************<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><!-- <xsl:output method="html" /> --> <xsl:template match="/"> <xsl:apply-templates select="BAPI_COMPANYCODE_GETLIST.Response"/> </xsl:template> <xsl:template match="BAPI_COMPANYCODE_GETLIST.Response"> <html> <head/> <body> <h1>Result from SAP query for Company CodesX</h1> <br/><!-- <p><xsl:value-of select="BAPI_COMPANYCODE_GETLIST.Response/RETURN/LOG_MSG_NO"/></p> --> <table border="1"> <tr> <th>Company Code</th> <th>Company Name</th> </tr> <xsl:apply-templates select="COMPANYCODE_LIST/item"/> </table> </body> </html> </xsl:template> <xsl:template match="COMPANYCODE_LIST/item"> <tr> <xsl:apply-templates select="COMP_CODE"/> <xsl:apply-templates select="COMP_NAME"/> </tr> </xsl:template>........*******************************</XSL>*********************************** Running a XML processor (Firefox) to my xml, nothing but the html structure was displayed, after millions of tests I've found that the issue is related to the xmlns attribute of the root node "<BAPI_COMPANYCODE_GETLIST.Response xmlns="urn:sap-com:document:sap:rfc:functions">", removing the xmlns attribute, my xsl is processed as expected (from me) and even the content of the table is displayed in the table.At this point I've tried to make some search but "xmlns" is not a "keyword" (it's in all the xml of the world!) and I can't find an answer..Do you know whether there's some restriction on parsing element with some attribute?Do you have any workaround to suggest? (I though to copy all the xml in a new one removing that attribute, but my knowledge is far from this result)..Thanks for any advice. Giovanni

  • Like 1
Link to post
Share on other sites

Well this is the FAQ with XSLT and XPath 1.0. The default namespace declaration (i.e. the xmlns="...") puts the elements in a namespace and to select and match them with XSLT/XPath 1.0 your stylesheet needs to declare a prefix for the namespace and then use that prefix in any XPath expression or XSLT match pattern to qualify element names e.g.

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:df="urn:sap-com:document:sap:rfc:functions"  version="1.0"  exclude-result-prefixes="df"><xsl:template match="/"><xsl:apply-templates select="df:BAPI_COMPANYCODE_GETLIST.Response"/></xsl:template><xsl:template match="df:BAPI_COMPANYCODE_GETLIST.Response">...</xsl:template></xsl:stylesheet> 

Note that your XML sample also has a lot of 'xmlns=""' on child or descendant elements, that way these are in no namespace and you will be able to select them without using a prefix.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...