Copy all elements of a namespace and nothing else

前端 未结 2 521
甜味超标
甜味超标 2021-01-25 03:36

We have a bunch of files that are html pages but which contain additional xml elements (all prefixed with our company name \'TLA\') to provide data and structure for an older pr

相关标签:
2条回答
  • 2021-01-25 03:50

    You could try something like this...

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:tla="http://www.tla.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@*|node()">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:template>
    
        <xsl:template match="tla:*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2021-01-25 04:00

    This should do it:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:tla="http://www.tla.com">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:template match="text()" />
    
      <xsl:template match="tla:* | tla:*/@* | tla:*/text()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    When run on your sample input (once the missing namespace declaration is added), the result is:

    <TLA:document xmlns:TLA="http://www.tla.com">
      <TLA:contexts>
        <TLA:context id="id_1" value="" />
      </TLA:contexts>
      <TLA:page>
        <TLA:question id="q_id_1" />
      </TLA:page>
    </TLA:document>
    
    0 讨论(0)
提交回复
热议问题