tags:

views:

62

answers:

2

OK, its Friday afternoon and I need to get this done:

The following xml needs to be transformed:

<?xml version="1.0" encoding="UTF-8"?>
<ProfiledSettings>
  <PropertySet File="properties.txt">
    <Property Name="scheduler.time">19h30</Property>
  </PropertySet>
  <PropertySet File="properties2.txt">
    <Property Name="inclusions.filters" />
    <Property Name="inclusions" />
  </PropertySet>
</ProfiledSettings>

to this:

<?xml version="1.0" encoding="UTF-8"?>
<ProfiledSettings>
  <PropertySet File="properties.txt">
    <Property Name="scheduler.time">19</Property>
  </PropertySet>
  <PropertySet File="properties2.txt">
    <Property Name="inclusions.filters" />
    <Property Name="inclusions" />
  </PropertySet>
</ProfiledSettings>

Notice that the '19h30' changed to '19'.

My xslt is not so good, but I know it should be simple.

What should the XSLT document look like to do this transform?

+2  A: 

The identity transform plus a template to match the property you want to change. The second template makes a copy of the input Property node, with all its attributes, and modifies the text contents.

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

    <xsl:template match="Property[@Name='scheduler.time']">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:value-of select="substring-before(text(),'h')"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Jim Garrison
Thank you, this looks promising. I have tested it, and it seems not to work though. I get an output identical to the input.
Ries
Ries
I got my answer based on your answer. Thanks!
Ries
I tested mine in Oxygen/XML, which uses Saxon for XSLT, and it worked as-is. Which XSLT processor are you using?
Jim Garrison
I tried also and it didn't work with IE but it worked with FF.
Filburt
For good-will's sake I have marked this as the answer. He did help me go home 'early' on a friday afternoon after all. Please note however my own answer as well.
Ries
A: 

This is what did work in the end:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Property[@Name='scheduler.time']">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:value-of select="substring-before(text(),'h')"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*">
      <xsl:copy>
      </xsl:copy>
    </xsl:template>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Ries
I can see no reason whatsoever that this should function any differently than what Jim Garrison posted.
Robert Rossney
Could it be the order of the templates?
Ries