Facedown
Facedown

Reputation: 192

Transforming XSL stored inside XML

Having following XML:

<xsl:element name="input">
    <xsl:attribute name="type">
        <xsl:text>email</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="name">
        <xsl:text>email</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="class">
        <xsl:text>form-control</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="required">
        <xsl:text>true</xsl:text>
    </xsl:attribute>
</xsl:element>

How can I pass it exactly like that to XSLT with following output:

<input type="email" name="email" class="form-control" required="true"/>

I have already tried xsl:copy-of and disable-output-escaping. But none of them seems to work. Is it even possible to achieve that?

Thanks.

Upvotes: 0

Views: 33

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

Given a well-formed XML input such as:

<xsl:element name="input" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:attribute name="type">
        <xsl:text>email</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="name">
        <xsl:text>email</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="class">
        <xsl:text>form-control</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="required">
        <xsl:text>true</xsl:text>
    </xsl:attribute>
</xsl:element>

you could use:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="xsl:element">
    <xsl:element name="{@name}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="xsl:attribute">
    <xsl:attribute name="{@name}">
        <xsl:apply-templates/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

to return:

<?xml version="1.0" encoding="UTF-8"?>
<input type="email" name="email" class="form-control" required="true"/>

As you can see, the fact that the input is an XSLT fragment is largely irrelevant here. The only difference it makes is that you don't need to define a new prefix for the input's namespace.

Upvotes: 3

Related Questions