2

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 Answer 1

1

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']"/>
2
  • Thank you for the answer but unfortunately it will not solve the problem. I didn't want to use 'j.4' in my select statement because it's "unknown" in advance. The only thing is known is the URI of that namespace.
    – omandryk
    Jul 21, 2018 at 5:10
  • Great! Thank you! It works as expected, but you've missed a closing bracket in the select statement. Could you please add it before I accept your answer?
    – omandryk
    Jul 21, 2018 at 22:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.