3

I'm trying to write my first XSLT. It needs to find all bind elements where the attribute ref begins with "$.root" and then insert ".newRoot". I have managed to match for the specific attribute, but I don't understand how I can get it to print an updated attribute value.

Input example XML:

<?xml version="1.0" encoding="utf-8" ?>
<top>
    <products>
        <product>
            <bind ref="$.root.other0"/>
        </product>
        <product>
            <bind ref="$.other1"/>
        </product>
        <product>
            <bind ref="$.other2"/>
        </product>
        <product>
            <bind ref="$.root.other3"/>
        </product>
    </products>
</top>

My XSL so far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
        <xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

The XML I would like to produce from the input:

<?xml version="1.0" encoding="utf-8" ?>
<top>
    <products>
        <product>
            <bind ref="$.newRoot.root.other0"/>
        </product>
        <product>
            <bind ref="$.other1"/>
        </product>
        <product>
            <bind ref="$.other2"/>
        </product>
        <product>
            <bind ref="$.newRoot.root.other3"/>
        </product>
    </products>
</top>

1 Answer 1

6

Instead of:

<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
    <xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute>
</xsl:template>

try:

<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
    <xsl:attribute name="ref">$.newRoot.root<xsl:value-of select="substring-after(., '$.root')" /></xsl:attribute>
</xsl:template>

or (same thing in a more convenient syntax):

<xsl:template match="bind/@ref[starts-with(., '$.root')]">
    <xsl:attribute name="ref">
        <xsl:text>$.newRoot.root</xsl:text>
        <xsl:value-of select="substring-after(., '$.root')" />
    </xsl:attribute>
</xsl:template>

Note the use of . to refer to the current node. In your version, the <xsl:value-of select="bind/@ref" /> instruction selects nothing, because the ref attribute is already the current node - and it has no children.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! If ref attribute is the current node, why do I have to name it in the xsl:attribute element?
@Björn If you prefer, you can calculate the name of the current node instead of specifying it literally: <xsl:attribute name="{name()}">.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.