xmlxslt-1.0

get all xml attributes from a node using xlst 1.0 transformation


I have the following xml and i would like to only filter the attributes of node "A" using XLST version 1.0 transformation

<A a_1="a1" a_2="a2"/>
<B>
 <C>
    hello
 </C>
</B>

I can use the following XLST transformation, but i'm looking if there is a way to get all the attributes without going one by one

<A a_1="{A/@a_1}" a_2="{A/@a_2}">

The output response would look like

<A a_1="a1" a_2="a2">

Solution

  • XSLT is very much sensitive to context. With a well-formed input such as:

    <A a_1="a1" a_2="a2">
      <B>
        <C>
        hello
     </C>
      </B>
    </A>
    

    you can use this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"  indent="yes"/>
    
    <xsl:template match="/A">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    to get:

    <?xml version="1.0"?>
    <A a_1="a1" a_2="a2"/>