Ismar Slomic
Ismar Slomic

Reputation: 5514

XPATH expression to get local name and namespace of XML attribute of datatype QName

I want to display XML document with XSLT by following this example from w3schools: http://www.w3schools.com/xml/xml_xsl.asp. The XSLT transformation to HTML will be done in the browser when opening the XML document.

Now, I have difficulties a) getting the local name and 2) getting the namespace of the attribute content of type QName in two separate expressions.

Example

<service xmlns:ns3="http://www.mycompany.com/" name="ns3:PersonService">
    <serviceInterface name="ns3:PersonServiceInterface">
         <operation>...</operation>
</serviceInterface>

Questions

  1. What XPATH expression will return PersonService as content of attribute name? <xsl:value-of select="@name"/> returns ns3:PersonService but i don't want the namespace prefix.
  2. What XPATH expression will return http://www.mycompany.com/ as the namespace of the attribute name?

Upvotes: 3

Views: 2881

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

1.What XPATH expression will return PersonService as content of attribute name?

Assuming that service is a child of the top element (you haven't provided a complete and well-formed XML document), use:

substring-after(/*/service/@name, ':')

2.What XPATH expression will return http://www.mycompany.com/ as the namespace of the attribute name?

Under the same assumptions as above, use:

/*/service/namespace::*[name() = substring-before(../@name, ':')]

XSLT-based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:value-of select="substring-after(/*/service/@name, ':')"/>
============
  <xsl:value-of select=
   "/*/service/namespace::*[name() = substring-before(../@name, ':')]"/>
 </xsl:template>
</xsl:stylesheet>

this XSLT transformation, when applied on the following XML document (the provided one, but completed):

<t>
    <service xmlns:ns3="http://www.mycompany.com/"
             name="ns3:PersonService">
        <serviceInterface name="ns3:PersonServiceInterface">
            <operation>...</operation>
        </serviceInterface>
    </service>
</t>

evaluates the two XPath expressions and copies the results of these evaluations to the output:

PersonService
============
http://www.mycompany.com/

Upvotes: 3

Related Questions