Thursday, 15 September 2011

xslt - Howto handle a XML with namespaces? -



xslt - Howto handle a XML with namespaces? -

if have xml:

<?xml version="1.0" encoding="utf-8"?> <env:envelope> <m:rgwspublicfirmactrtuser> <m:firmactdescr>ΑΛΛΟ ΛΙΑΝΙΚΟ ΕΜΠΟΡΙΟ ΣΕ ΜΗ ΕΞΕΙΔΙΚΕΥΜΕΝΑ ΚΑΤΑΣΤΗΜΑΤΑ</m:firmactdescr> <m:firmactkind>2</m:firmactkind> <m:firmactkinddescr>ΔΕΥΤΕΡΕΥΟΥΣΑ</m:firmactkinddescr> <m:firmactcode>47191000</m:firmactcode> </m:rgwspublicfirmactrtuser> <m:rgwspublicfirmactrtuser> <m:firmactdescr>ΛΙΑΝΙΚΟ ΕΜΠΟΡΙΟ ΕΙΔΩΝ ΔΩΡΩΝ ΓΕΝΙΚΑ</m:firmactdescr> <m:firmactkind>2</m:firmactkind> <m:firmactkinddescr>ΔΕΥΤΕΡΕΥΟΥΣΑ</m:firmactkinddescr> <m:firmactcode>47191008</m:firmactcode> </m:rgwspublicfirmactrtuser> </env:envelope>

and utilize xslt:

<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <h3>Δραστηριότητες</h3> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Δραστηριότητα</th> <th style="text-align:left">Αριθμός δραστηριότητας</th> <th style="text-align:left">Περιγραφή δραστηριότητας</th> <th style="text-align:left">Κωδικός δραστηριότητας</th> </tr> <xsl:for-each select="envelope/rgwspublicfirmactrtuser"> <tr> <td><xsl:value-of select="firmactdescr"/></td> <td><xsl:value-of select="firmactkind"/></td> <td><xsl:value-of select="firmactkinddescr"/></td> <td><xsl:value-of select="firmactcode"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

all works fine, when have namespace in input xml:

<env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://gr/gsis/rgwspublic/rgwspublic.wsdl" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">

instead of no namespaces:

<env:envelope>

the xslt won't work

my problem xml received 3rd party , can't command content. need process is. maybe can replace big env:envelope , little env:envelope within within server process, there anyway can create xslt work without changing xml?

to match namespaced elements in xslt have declare namespace prefix in stylesheet same namespace uri, , utilize prefix in xpath expressions:

<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m="http://gr/gsis/rgwspublic/rgwspublic.wsdl"> <xsl:template match="/"> <!-- ... --> <xsl:for-each select="env:envelope/m:rgwspublicfirmactrtuser">

(m: namespace uri taken comment). namespace uris not have real urls browser can fetch, treated unique identifiers namespace. xml technologies utilize "urn" identifiers namespaces (like urn:example:namespace) instead of http urls.

xml xslt xml-namespaces

No comments:

Post a Comment