代码之家  ›  专栏  ›  技术社区  ›  Ries

简单XSL转换

  •  0
  • Ries  · 技术社区  · 16 年前

    好的,今天是星期五下午,我需要完成这项工作:

    需要转换以下XML:

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

    对此:

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

    请注意,“19H30”更改为“19”。

    我的XSLT不太好,但我知道它应该很简单。

    在进行这种转换时,XSLT文档应该是什么样子的?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Jim Garrison    16 年前

    标识转换加上模板以匹配要更改的属性。第二个模板复制输入属性节点及其所有属性,并修改文本内容。

    <?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>
    
        2
  •  0
  •   Ries    16 年前

    这就是最终的工作:

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