in XSLT, How to find the namespace of an XML element attribute?
Trying to use the function namespace-uri to find the namespace of an attribute.
<xsl:template match="xse:seeAlso">
<xsd:xmlEntityReference xml:space="preserve">
<xsl:value-of select="namespace-uri()"/>#E/<xsl:value-of select="@ref"/>
</xsd:xmlEntityReference>
</xsl:template>
I want the namespace for the "ref" attribute of the element. Any tip?
UPDATE 1 To make it clearer, I want the namespace of the element referenced by the "ref" attribute. Sorry for any confusion.
UPDATE 2 @empo: This is snippet of one of the schemas
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:html="http开发者_运维知识库://www.w3.org/1999/xhtml"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xse="http://schemas.microsoft.com/wix/2005/XmlSchemaExtension"
targetNamespace="http://schemas.microsoft.com/wix/IIsExtension"
xmlns="http://schemas.microsoft.com/wix/IIsExtension">
<xs:import namespace="http://schemas.microsoft.com/wix/2006/wi" />
<xs:element name="Certificate">
<xs:annotation>
<xs:documentation>
Used to install and unintall certificates.
</xs:documentation>
<xs:appinfo>
<xse:seeAlso ref="CertificateRef"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<!-- Cut off -->
</xs:complexType>
</xs:element>
<xs:element name="CertificateRef">
<xs:annotation>
<xs:documentation>
...
</xs:documentation>
<xs:appinfo>
<xse:seeAlso ref="Certificate"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<!-- Cut off -->
</xs:complexType>
</xs:element>
</xs:schema>
This is a sample of the transform
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:xsd="http://schemas.xsddoc.codeplex.com/schemaDoc/2009/3"
xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xse="http://schemas.microsoft.com/wix/2005/XmlSchemaExtension"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="msxsl xs xse">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="parentItemType"/>
<xsl:param name="parentItemNamespace"/>
<xsl:param name="parentItemUri"/>
<xsl:param name="currentItemType"/>
<xsl:param name="currentItemNamespace"/>
<xsl:param name="currentItemUri"/>
<xsl:template match="*">
<xsl:apply-templates select="xs:annotation" />
</xsl:template>
<xsl:template match="xse:seeAlso">
<xsd:xmlEntityReference xml:space="preserve">
<xsl:value-of select="namespace-uri()"/>#E/<xsl:value-of select="@ref"/>
</xsd:xmlEntityReference>
</xsl:template>
</xsl:stylesheet>
If the value of the @ref
attribute is selected as shown, then the namespace URI value would be nothing. The attribute would be in the "unnamed namespace" (or "null namespace") and it won't have a namespace URI value.
If an attribute is bound to a namespace, then it will have a namespace prefix and would need to be addressed as such in the XPath (e.g. @foo:ref
).
You can verify this and obtain the namespace URI of the attribute(or any element or attribute node) passing it as a parameter to the namespace-uri() function.
namespace-uri(@ref)
To make it clearer, I want the namespace of the element referenced by the "ref" attribute. Sorry for any confusion
You are searching for following
<xsl:value-of select="namespace-uri(//*[@name=current()/@ref])"/>
or, even:
<xsl:value-of select="namespace-uri(//xs:element[@name=current()/@ref])"/>
精彩评论