w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Extract element and attribute of an XML file

Here's how I tried to solve it, JAXP was used to render (transform) the result:

XML source:

<?xml version="1.0" encoding="UTF-8"?>
<HTML>
    <HEAD>
        <BASE href="hrEf" target="tArgEt"/>
        <SCRIPT type="typE" src="src"/>
        <STYLE type="typE" media="mEdIA"/>
    </HEAD>
    <BODY onload="OnlOAd" onunload="OnUnlOAd"
background="bAckgrOUnd"/>
</HTML>

XSL template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

    <xsl:output
        method="text"
        encoding="UTF-8"
        omit-xml-declaration="yes"
        indent="no"
        media-type="text/plain"
    />

    <xsl:template match="*">
        <xsl:value-of select ="local-name(.)"/>
        <xsl:for-each select="attribute::*">
            &#160;&#64;<xsl:value-of
select="local-name(.)"/>&#61;&#34;<xsl:value-of
select="."/>&#34;
        </xsl:for-each>
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

And the result:

HTML
    HEAD
        BASE
             @href="hrEf"

             @target="tArgEt"

        SCRIPT
             @src="src"

             @type="typE"

        STYLE
             @media="mEdIA"

             @type="typE"


    BODY
             @background="bAckgrOUnd"

             @onload="OnlOAd"

             @onunload="OnUnlOAd"

Notice how it works regardless the depth of the tree; cheers!





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.