Re: [xsl] What is the appropriate XPath 2.0 expression to reference an XSD simpleType ?

Subject: Re: [xsl] What is the appropriate XPath 2.0 expression to reference an XSD simpleType ?
From: "Costello, Roger L. costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 29 Dec 2015 18:16:31 -0000
Dan Sullivan wrote:

> Are you looking for something like
>
> *[local-name()='base' and (namespace-uri()='' or
namespace-uri()='http://www.w3.org/2001/XMLSchema']

That is a good suggestion Dan. Thank you. However, that XPath returns a
simpleType not a QName (which is what I desire).

I wrote an XSLT program which returns the appropriate QName. See below. It is
awful (i.e., it is a very complicated solution to a very simple problem). I am
hoping that someone can provide a simple solution.  /Roger

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                        xmlns:g="function"
                        xmlns:xs="http://www.w3.org/2001/XMLSchema";
                       exclude-result-prefixes="#all"
                       version="2.0">

    <xsl:output method="text" />

    <xsl:template match="xs:schema">
        <xsl:message>
            <xsl:text>The appropriate prefix:local-name is: #</xsl:text>
            <xsl:value-of select="g:QName-for-local-name('base', .)"/>
            <xsl:text>#</xsl:text>
        </xsl:message>
    </xsl:template>

    <xsl:function name="g:QName-for-local-name">
        <xsl:param name="local-name" as="xs:string" />
        <xsl:param name="schema" as="element(xs:schema)" />

        <xsl:variable name="tns" select="$schema/@targetNamespace"
as="xs:anyURI?"/>

        <xsl:variable name="prefix" select="g:namespace-prefix-for-uri($tns,
$schema)" as="xs:string?"/>
        <xsl:choose>
            <xsl:when test="not($tns)">
                <xsl:value-of select="QName('', $local-name)"/>
            </xsl:when>
            <xsl:when test="not($prefix)">
                <xsl:value-of select="QName($tns, $local-name)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="QName($tns, concat($prefix, ':',
$local-name))"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>

    <xsl:function name="g:namespace-prefix-for-uri" as="xs:string?">
        <xsl:param name="uri" as="xs:anyURI?" />
        <xsl:param name="schema" as="element(xs:schema)" />

        <xsl:choose>
            <xsl:when test="not($uri)" />
            <xsl:otherwise>
                <xsl:variable name="prefixes"
select="in-scope-prefixes($schema)"/>
                <xsl:value-of
select="g:auxiliary-namespace-prefix-for-uri($uri, $prefixes, $schema)"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>

    <xsl:function name="g:auxiliary-namespace-prefix-for-uri"
as="xs:string?">
        <xsl:param name="uri" as="xs:anyURI" />
        <xsl:param name="prefixes" as="xs:string*" />
        <xsl:param name="schema" as="element(xs:schema)" />

        <xsl:choose>
            <xsl:when test="count($prefixes) eq 0" />
            <xsl:when test="namespace-uri-for-prefix($prefixes[1], $schema) eq
$uri">
                <xsl:value-of select="$prefixes[1]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of
select="g:auxiliary-namespace-prefix-for-uri($uri, $prefixes[position() gt 1],
$schema)"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:function>

</xsl:stylesheet>

Current Thread