Get xml attribute named xlink: href using xsl

How can I get the xlink:href attribute value for xml node in xsl template?

I have this xml node:

 <DCPType> <HTTP> <Get> <OnlineResource test="hello" xlink:href="http://localhost/wms/default.aspx" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" /> </Get> </HTTP> </DCPType> 

When I try to execute the following xsl, I get the error "Prefix" xlink "not defined".

 <xsl:value-of select="DCPType/HTTP/Get/OnlineResource/@xlink:href" /> 

When I try this simple attribute, it works:

 <xsl:value-of select="DCPType/HTTP/Get/OnlineResource/@test" /> 
+4
source share
2 answers

You need to declare the XLINK namespace in XSLT before you can reference it.

You can add it to the xsl:value-of element:

 <xsl:value-of select="DCPType/HTTP/Get/OnlineResource/@xlink:href" xmlns:xlink="http://www.w3.org/1999/xlink" /> 

However, if you need to reference it in other areas of your stylesheet, then it would be easier to declare it at the top in your XSLT document element:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink"> 

By the way, you do not need to use the same namespace prefix in the stylesheet as in your XML. The namespace prefix is ​​used only as an abbreviation for the namespace URI. You can declare and use the XLINK namespace as follows:

 <xsl:value-of select="DCPType/HTTP/Get/OnlineResource/@x-link:href" xmlns:x-link="http://www.w3.org/1999/xlink"/> 
+7
source

While @ Mads-Hansen provided a good answer, it's good to know that there is an alternative way to refer to names that are in the namespace :

In this case, you can also get an attribute with the following XPath expression:

  DCPType/HTTP/Get/OnlineResource/@* [namespace-uri() = 'http://www.w3.org/1999/xlink'] 
+3
source

Source: https://habr.com/ru/post/1308974/


All Articles