3

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>
2
  • 3
    Maybe post a fragment of the XML you are using, as well. Commented Aug 31, 2011 at 1:29
  • @Cheeso: I am processing the WiX installer schema to extract the documentation/annotation information from it. It uses a custom linking between elements (via that xse:seeAlso etc). Commented Sep 1, 2011 at 4:12

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

It looks possible to use namespace-uri(@*[local-name()='ref']) construct to cover both cases (@ref and @xse:ref).
1

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])"/>

5 Comments

Thanks for the reply. This, however, returns empty string.
I have now posted parts of the sources, if that will help.
Thank you, but this will not work since it is just for the above case. There are 13 schemas defined in different namespaces, so it must be a general expression that could work on all cases not the particular case shown above.
I've just answered your question, I can't read in your mind. This is just an example based on the information you have provided. You should be able to fix your problem now by yourself. We can't help you more without a precise definition of your requirements.
However the expression above is general enough to work with other schemas, it depends by the context where it is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.