1

This is the XML block:

<Object type="proto">
    <Name value="test1"/>
    <Enabled value="1"/>
    <System value="active"/>
    <Time value="10"/>
</Object>
<Object type="proto">
    <Name value="test2"/>
    <Enabled value="1"/>
    <System value="active"/>
    <Time value="20"/>
</Object>

How do I change the value of 'Time' only for 'test1' during a copy?

luigi
  • 35
  • 2
  • 7
  • Show how the expected output looks like, and what you have so far in term of XSL codes – har07 Jul 22 '15 at 07:55
  • I already have the base identity copy, which will copy the entire block with its nodes and attributes. But I want to change the value of Time for test1, say fro 10 to 30. I need to understand how to specify that the change has to affect only the test1 block. – luigi Jul 22 '15 at 08:17
  • Next time please post XSL codes you already have, so that people can base their answer on that XSL. This way, to mention some of many benefits, hopefully the answer will be easier to understand for you, and easier to write for the helper. Thanks – har07 Jul 22 '15 at 11:29

1 Answers1

2

This is one possible way :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="Object[Name/@value='test1']/Time">
      <xsl:copy>
        <xsl:attribute name="value">30</xsl:attribute>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Brief explanation regarding xsl:templates being used :

  • <xsl:template match="@* | node()">... : Identity template; copies nodes and attributes to the output XML, unchanged.
  • <xsl:template match="Object[Name/@value='test1']/Time">... : Overrides identity template for <Time> element, that is direct child of <Object> having child Name/@value equals test1. This template copies the matched <Time> element and change the attribute value to 30.
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thank you very much!!! I didn't know you could specify 'Object[Name/@value='test1']/' in match – luigi Jul 22 '15 at 18:59