0

I want to modify existing XML file using XSLT. The original XML file is

<?xml version="1.0" encoding="UTF-8"?>
<entry>
    <object>
        <items>
            <item>
                <name>John Doe</name>    <!-- Changed tag to closing tag -->
                <public-url>http://www.johndoe.com</public-url>
            </item>
        </items>
        <records>
            <record>
                <person>
                    <field name="book">
                        <text>A book</text>
                        <links>
                            <link>http://www.acook.com</link>
                        </links>
                    </field>
                </person>
            </record>
        </records>
    </object>
</entry>

Now I want to use XSL to get the the user info from <item> and insert a new <field> node into <person>. The final result will look like this.

<?xml version="1.0" encoding="UTF-8"?>
<entry>
    <object>
        <items>
            <item>
                <name>John Doe<name>
                <public-url>http://www.johndoe.com</public-url>
            </item>
        </items>
        <records>
            <record>
                <person>
                    <field name="author">
                        <text>John Doe</text>
                        <links>
                            <link>http://www.johndoe.com</link>
                        </links>
                    </field>
                    <field name="book">
                        <text>A book</text>
                        <links>
                            <link>http://www.acook.com</link>
                        </links>
                    </field>
                </person>
            </record>
        </records>
    </object>
</entry>

Below is my attempt, I want to get the <name> and <public-url> value from the <item> and become two variables. Create a new <field> using these two variables and insert into <record>. Currently I can't figure out a way to insert this new node into the correct location.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
    <xsl:copy>
     <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="item/">
    <xsl:variable name="username" select="@name" />
    <xsl:variable name="userurl" select="@public-url" />
    <xsl:copy-of select="."/>
    <field name="author">
    <text><xsl:value-of select="$username"/></text>
    <links>
         <link>
             <xsl:value-of select="$userurl" />
         </link>
    </links>
    </field>
</xsl:template>
</xsl:stylesheet>

Please advices, Thank you!

3
  • How come the input has <name>John Doe<name> and the new field has a different name <text>John Dow</text>? Commented Sep 14, 2018 at 19:16
  • It was a typo. The value in <text> is from <name> Commented Sep 14, 2018 at 19:56
  • I corrected the <name>...</name> situation. Commented Sep 14, 2018 at 20:38

1 Answer 1

1

This should do it.

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

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="person">
    <xsl:element name="person">
        <xsl:element name="field">
            <xsl:attribute name="name">
                <xsl:text>author</xsl:text>
            </xsl:attribute>
            <xsl:element name="text">
                <xsl:value-of select="./ancestor::object/items/item/name"/>
            </xsl:element>
            <xsl:element name="links">
                <xsl:element name="link">
                    <xsl:value-of select="./ancestor::object/items/item/public-url"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It refresh my memory about ancestor

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.