mmirwaldt
mmirwaldt

Reputation: 883

How can you copy all attributes and set them on a same new value when you copy a xml-node?

How can copy all attributes and set them on the same value when I copy a xml-node?

Given a xml:

<schedule>
    <owner>
        <name>
            <first>Eric</first>
        </name>
    </owner>
    <appointment>
        <when>
            <date month="03" day="15" year="2001"/>
        </when>
        <subject>Interview potential new hire</subject>
    </appointment>
</schedule>

What I want to get after the transformation:

    <schedule>
        <owner>
            <name>
                <first>?</first>
            </name>
        </owner>
        <appointment>
            <when>
                <date month="?" day="?" year="?"/>
            </when>
            <subject>?</subject>
        </appointment>
   </schedule>

That's what I managed:

 <xsl:template match="*|@*">
        <xsl:copy>
                <xsl:if test="node() and not(*)">
                    <xsl:text>?</xsl:text>
                </xsl:if>
                <xsl:if test="not(node())">
                    <xsl:attribute name="???">?</xsl:attribute>
                </xsl:if>
                <xsl:apply-templates select="*|@*" />
            </xsl:copy>
        </xsl:template>

    </xsl:stylesheet>

These do not work:

<xsl:attribute name="name(.)">?</xsl:attribute> 
<xsl:attribute name="@*">?</xsl:attribute>
<xsl:attribute name="*">?</xsl:attribute>
<xsl:attribute name=".">?</xsl:attribute>       

<xsl:if test="not(node())">
     <xsl:text>?</xsl:text>
</xsl:if>

<xsl:if test="not(node())">
     ?
</xsl:if>

Does the attribute name always expect a static value? Is there a workaround in xslt?

Upvotes: 1

Views: 1266

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

For an attribute, you can reconstruct it using name() (or local-name()).

Example:

XML Input

<schedule>
    <owner>
        <name>
            <first>Eric</first>
        </name>
    </owner>
    <appointment>
        <when>
            <date month="03" day="15" year="2001"/>
        </when>
        <subject>Interview potential new hire</subject>
    </appointment>
</schedule>

XSLT 1.0

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

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*" priority="1">
        <xsl:attribute name="{name()}">
            <xsl:text>?</xsl:text>
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="text()" priority="1">
        <xsl:text>?</xsl:text>
    </xsl:template>

</xsl:stylesheet>

XML Output

<schedule>
   <owner>
      <name>
         <first>?</first>
      </name>
   </owner>
   <appointment>
      <when>
         <date month="?" day="?" year="?"/>
      </when>
      <subject>?</subject>
   </appointment>
</schedule>

Upvotes: 1

Related Questions