开发者

Simple XSL transform

开发者 https://www.devze.com 2022-12-18 00:20 出处:网络
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\"?>

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 shou开发者_运维技巧ld be simple.

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


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>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消