Proposed Answer Remove DTD and XMNS in xml file using XSLT

  • Saturday, November 24, 2012 2:45 PM
     
     
    Hi,

    I want to DTD and XMLNS in xml file useing xslt.

    XMl Document:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE Report SYSTEM "https://test.com/test/reports/dtd/tdr_1_11.dtd">
    <Report Name="Transaction Detail"
            Version="1.11"
            xmlns="https://test.com/test/reports/dtd/tdr_1_11.dtd"
            AccountID="testaccount"
            ReportStartDate="2012-11-21T07:00:00"
            ReportEndDate="2012-11-22T07:00:00">
        <Requests>
        </Requests>
    </Report>


    Result:


    <?xml version="1.0" encoding="utf-8"?>
    <Report Name="Transaction Detail"
            Version="1.11"
            AccountID="testaccount"
            ReportStartDate="2012-11-21T07:00:00"
            ReportEndDate="2012-11-22T07:00:00">
        <Requests>
        </Requests>
    </Report>

    Can you please share the xslt for the above one.?

All Replies

  • Saturday, November 24, 2012 3:02 PM
     
     Proposed Answer Has Code

    The XSLT data model does not include a DTD so that would be dropped anyway by XSLT processing. As for the namespace it should suffice to use three templates

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
    <xsl:template match="*">
      <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
    </xsl:template>
    
    <xsl:template match="@*">
      <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:template>
    
    <xsl:template match="text() | comment() | processing-instruction()">
      <xsl:copy/>
    </xsl:template>
    
    </xsl:stylesheet>

    Note that with both the default XML parsing in .NET as well as with MSXML 6 DTD processing needs to be explicitly allowed or ignored so depending on your XSLT processor and XML parser to process a document liked you posted with a DTD referenced you need to enable DTD processing, for instance with .NET by setting XmlReaderSettings.DtdProcessing to Ignore or Parse.


    MVP Data Platform Development My blog

    • Proposed As Answer by Martin Honnen Saturday, November 24, 2012 6:38 PM
    •