English 中文(简体)
get xml attribute named xlink:href using xsl
原标题:

How can I get the value of an attribute called xlink:href of an 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 the following xsl, I get an error saying "Prefix xlink is 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" />
最佳回答

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

You could 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 are going to need to reference it in other areas of your stylesheet, then it would be easier to declare it at the top in the document element of your XSLT:

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

Incidentally, you don t need to use the same namespace prefix in your stylesheet as what is used in your XML. The namespace prefix is just used as shorthand for the namespace URI. You could declare and use the XLINK namespace like this:

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

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

In this case, you could also acces the attribute with the following XPath expression:

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




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签