How do I force the XSLT result tree to serialize with a specific default namespace?

I am trying to remove namespace classifiers from a document, while preserving the default document namespace:

<foo:doc xmlns:foo='somenamespace'>
    <foo:bar />
</foo:doc>

For

<doc xmlns='somenamespace'>
    <bar/>
</doc>

(I know this is pointless, but our client does not receive XML and uses string comparisons to find information in a document.)

I am using the Java JAXP Transformer API to get my work done here. I can remove all namespace information using this stylesheet, but instead I want to force serialization without prefixes:

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

      <xsl:output omit-xml-declaration='yes' indent='yes'/>

      <xsl:template match='@*|node()'> 
        <xsl:copy> 
          <xsl:apply-templates select='@*|node()' /> 
        </xsl:copy> 
      </xsl:template> 

      <xsl:template match='*'> 
        <xsl:element name='{local-name()}'> 
          <xsl:apply-templates select='@*|node()' /> 
        </xsl:element> 
      </xsl:template> 
</xsl:stylesheet>

How can i do this?

+3
source share
2 answers

, "somenamespace", , "somenamenamespace" ( ) : xmlns = 'somenamespace

, local-name(), , :

<doc xmlns="somenamespace">
    <bar/>
</doc>

, ?
"node()" "*" .

node() - : "* | text() | comment() | processing-instruction()"

, :

1.) "@* | node()", , node.

<?xml version='1.0' encoding='UTF-8'?> 
<xsl:stylesheet 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'  
    xmlns:xs='http://www.w3.org/2001/XMLSchema' 
    xmlns='somenamespace'
    exclude-result-prefixes='xs' 
    version='2.0'> 

    <xsl:output omit-xml-declaration='yes' indent='yes'/>

    <xsl:template match='@*|text()|comment()|processing-instruction()'> 
        <xsl:copy> 
            <xsl:apply-templates select='@*|node()' /> 
        </xsl:copy> 
    </xsl:template> 

    <xsl:template match='*' > 
        <xsl:element name='{local-name()}'> 
            <xsl:apply-templates select='@*|node()' /> 
        </xsl:element> 
    </xsl:template> 

</xsl:stylesheet>

2.) ", , " @| node()".

<?xml version='1.0' encoding='UTF-8'?> 
<xsl:stylesheet 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'  
    xmlns:xs='http://www.w3.org/2001/XMLSchema'
    xmlns='somenamespace' 
    exclude-result-prefixes='xs' 
    version='2.0'> 

    <xsl:output omit-xml-declaration='yes' indent='yes'/>

    <xsl:template match='@*|node()'> 
        <xsl:copy> 
            <xsl:apply-templates select='@*|node()' /> 
        </xsl:copy> 
    </xsl:template> 

    <xsl:template match='*' priority="1"> 
        <xsl:element name='{local-name()}'> 
            <xsl:apply-templates select='@*|node()' /> 
        </xsl:element> 
    </xsl:template> 

</xsl:stylesheet>
+2

, "sn:*" "*"

<xsl:namespace-alias stylesheet-prefix="sn" result-prefix="#default"/>

? ( xslns: sn = "somenamespace" )

+2

Source: https://habr.com/ru/post/1715272/


All Articles