How to remove namespace prefix leaving namespace value (XSLT)?

前端 未结 2 665
青春惊慌失措
青春惊慌失措 2020-12-21 21:57

I know how to remove namespaces at all but what I need to do is only to remove specific namespace prefixes eg transform this file (removing xenc prefixes):

&         


        
相关标签:
2条回答
  • 2020-12-21 22:07

    Try the following stylesheet. It contains the identity transform and a template to strip the namespace of xenc:* elements. Note that xenc:* attributes are not handled.

    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
    
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="xenc:*">
        <xsl:element name="{local-name()}" namespace="http://www.w3.org/2001/04/xmlenc#">
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2020-12-21 22:25

    Nearly the same solution as from nwellnhof. But make use of default namesepace in stylesheet. Add: xmlns="http://www.w3.org/2001/04/xmlenc#".

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
                    xmlns="http://www.w3.org/2001/04/xmlenc#"  >
        <xsl:output indent="yes"/>
    
        <xsl:template match="xenc:*">
            <xsl:element name="{local-name()}" >
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
    
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题