2
votes

I have a source XML file with dynamic (generated) namespace prefix but static URI for that namespace. I need to get this generated prefix by URI and use it in my XSL style sheet.

Source XML:

<rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.4="http://www.w3.org/2004/02/skos/core#">
    <rdf:Description>
        <j.4:prefLabel>TestNode</j.4:prefLabel>
    </rdf:Description>
</rdf:RDF>

XSL Stylesheet :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
                xmlns:skos="http://www.w3.org/2004/02/skos/core#"
                xmlns:j.4="http://www.w3.org/2004/02/skos/core#"
                exclude-result-prefixes="xsl skos rdf xsd "
                version="1.0">

    <xsl:output method="xml" version="1.0" encoding="UTF-8"
                indent="yes" omit-xml-declaration="yes" />

    <xsl:variable name="skosprefix" select="name(//rdf:RDF/namespace::*[. = 'http://www.w3.org/2004/02/skos/core#'])" />

    <xsl:template match="//rdf:RDF/rdf:Description">
        <node>
            <xsl:value-of select="j.4:prefLabel"/>
        </node>
    </xsl:template>

</xsl:stylesheet>

So in my XSL stylesheet (select statement) I would like to use something like $skosprefix:prefLabel instead of j.4:prefLabel to get the resulting XML:

<node>TestNode</node>

How can I achieve it using XSLT 1.0?

1

1 Answers

1
votes

You could use something like

<xsl:value-of select="*[name() = 'j.4:prefLabel'"/>

If you have the namespace in a variable (here, nsuri), then you can use this:

<xsl:value-of select="*[namespace-uri() = $nsuri and local-name() = 'prefLabel']"/>