0

I have the following xml file

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement"    
name="test">
    <content>
        <on> false </on>
    </content>
</application>

I want to have my xsl modify the xml so that:

If a tag does exist with the same name, then overwrite the value.

If a tag does not exist with the same value, then insert it in.

Here is my xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement"
    xmlns="http://www.w3.org/1999/xhtml" version="1.0">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="content">
        <xsl:copy>
            <xsl:apply-templates/>
            <xsl:if test="not(status)">
                <status>new status </status>
            </xsl:if>
            <xsl:if test="on">
                <on>new on value</on>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I am having problems with the <on>new on value</on>, as the solution should replace the on tag value, but instead creates an entirely new one

The result is the following (without the top xml and application tags):

<content>
    <on> false </on>
    <value> light </value>
    <status xmlns="http://www.w3.org/1999/xhtml">new status 

    </status>
    <on xmlns="http://www.w3.org/1999/xhtml">new on value

    </on>
</content>

How can I replace the on tag, in the same template?

2
  • inside your copy you are telling it <xsl:apply-templates/> to process all of the child nodes of content. If you don't want it to process the exisitng on tag you'll need to exclude that from the copy. If you don't want to copy other child nodes you could skip that line. Commented Mar 1, 2018 at 0:05
  • The problem with leaving it out is that if in my original xml, I had a tag that said <another> one </another> and I leave out the apply-templates, then that won't be copied Commented Mar 1, 2018 at 0:10

1 Answer 1

2

You can use template matching to match the <on> element and the <status> element (not part of the example).

<xsl:template match="content">                  <!-- remove <xsl:if...> from this template -->
    <xsl:copy>
        <xsl:apply-templates />
        <xsl:if test="not(status)">             <!-- if <status> element does not exist, create one -->
            <status>added new status </status>
        </xsl:if>
        <xsl:if test="not(on)">                 <!-- if <on> element does not exist, create one -->
            <on>added new on value</on>
        </xsl:if>
    </xsl:copy>
</xsl:template>

<xsl:template match="on[parent::content]">      <!-- replaces <on> elements with parent <content> -->
    <on>new on value</on>
</xsl:template>

<xsl:template match="status[parent::content]">  <!-- replaces <status> elements with parent <content> -->  
    <status>new status </status>
</xsl:template>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:t="http://www.tibco.com/xmlns/ApplicationManagement" name="test">
    <content>
        <on xmlns="http://www.w3.org/1999/xhtml">new on value</on>        
        <status xmlns="http://www.w3.org/1999/xhtml">added new status </status>
    </content>
</application>
Sign up to request clarification or add additional context in comments.

1 Comment

How would you do it if you wanted to insert on, in the event that it does not exist?

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.