替换所选值中的字符

时间:2017-05-23 13:22:19

标签: xslt replace

我是xslt中的新手,我必须将xml文件转换为html。我能够检索我的信息,但我想收缩一些字符串。我不知道如何将xsl模板调用到 value-of select 调用。有人能帮助我吗?

编辑:我更改了我的rplace模板

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>Changelog</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:center">Action</th>
      <th style="text-align:center">Author</th>
      <th style="text-align:center">Path</th>
      <th style="text-align:center">Filename</th>
      <th style="text-align:center">Comment</th>
    </tr>
    <xsl:for-each select="//Field[@name='CPEntries']/List/Item">
        <tr>
        <td><xsl:value-of select="Field[@name='type']/Item/@id"/></td>
        <td><xsl:value-of select="Field[@name='user']/Item/@id"/></td>
        <td><xsl:value-of select="Field[@name='member']/Item/@id"/></td>
        <td><xsl:value-of select="Field[@name='summary']"/></td>

        <td>

              <xsl:call-template name="string-replace-all">
                  <xsl:with-param name="input" select="Field[@name='configpath']/Item/@id"/>
                  <xsl:with-param name="replace" select="'E'" />
                  <xsl:with-param name="by" select="'...'" />
              </xsl:call-template>

        </td>

        </tr>
    </xsl:for-each>
  </table>

</body>
</html>

</xsl:template>

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = ''or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


</xsl:stylesheet>




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

      <table border="1">
        <tr bgcolor="#9acd32">
          <th style="text-align:center">Action</th>
          <th style="text-align:center">Author</th>
          <th style="text-align:center">Path</th>
          <th style="text-align:center">Filename</th>
          <th style="text-align:center">Comment</th>
        </tr>
        <xsl:for-each select="//Field[@name='CPEntries']/List/Item">
            <tr>
            <td><xsl:value-of select="Field[@name='type']/Item/@id"/></td>
            <td><xsl:value-of select="Field[@name='user']/Item/@id"/></td>
            <td><xsl:value-of select="Field[@name='member']/Item/@id"/></td>
            <td><xsl:value-of select="Field[@name='summary']"/></td>

            <td>
                <xsl:call-template name="search-and-replace">
                <xsl:with-param name="input" select=value-of select="Field[@name='configpath']/Item/@id"/>
                <xsl:with-param name="search-string" select="STRINGTOSEARCHFOR"/>
                <xsl:with-param name="replace-string" select="..."/>
                </xsl:call-template>
            </td>

            </tr>
        </xsl:for-each>
      </table>

    </body>
    </html>

    </xsl:template>

    <xsl:template name="search-and-replace">
  <xsl:param name="input"/>
  <xsl:param name="search-string"/>
  <xsl:param name="replace-string"/>
  <xsl:choose>
    <xsl:when test="$search-string and contains($input,$search-string)">
      <xsl:value-of select="substring-before($input,$search-string)"/>
      <xsl:value-of select="$replace-string"/>
      <xsl:call-template name="search-and-replace">
        <xsl:with-param name="input" select="substring-after($input,$search-string)"/>
        <xsl:with-param name="search-string" select="$search-string"/>
        <xsl:with-param name="replace-string" select="$replace-string"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$input"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


    </xsl:stylesheet>

编辑2:这是我的xml文件的示例条目

<Field name="CPEntries">
        <List elementType="item">
          <Item id="PATH" context="775412:4" modelType="si.ChangePackage.Entry" displayId="PATH:Update">
            <Field name="type">
              <Item id="Update" modelType="si.ChangePackage.Entry.Action" displayId="Update">
                <Field name="action">
                  <Value dataType="string">Update</Value>
                </Field>
                <Field name="previousState">
                  <Item id="FILE.c" modelType="cpName" displayId="FILE.c">
                    <Field name="oldRevision">
                      <Item id="1.63" modelType="si.Revision" displayId="1.63">
                      </Item>
                    </Field>
                  </Item>
                </Field>
                <Field name="isCommitted">
                  <Value dataType="boolean">true</Value>
                </Field>
                <Field name="isDeferred">
                  <Value dataType="boolean">false</Value>
                </Field>
                <Field name="isDiscarded">
                  <Value dataType="boolean">false</Value>
                </Field>
                <Field name="isPending">
                  <Value dataType="boolean">false</Value>
                </Field>
                <Field name="isExclusive">
                  <Value dataType="boolean">false</Value>
                </Field>
              </Item>
            </Field>
            <Field name="membertype">
              <Value dataType="string">Member</Value>
            </Field>
            <Field name="member">
              <Item id="FILE.c" modelType="si.Member" displayId="FILE.c">
              </Item>
            </Field>
            <Field name="revision">
              <Item id="1.64" modelType="si.Revision" displayId="1.64">
              </Item>
            </Field>
            <Field name="user">
              <Item id="Lastname, Firstname (uid)" modelType="si.User" displayId="Lastname, Firstname (uid)">
                <Field name="fullname">
                  <Value dataType="string"></Value>
                </Field>
              </Item>
            </Field>
            <Field name="timestamp">
              <Value dataType="datetime">2017-05-23T09:09:40</Value>
            </Field>
            <Field name="project">
              <Item id="PROJECTPATH" modelType="si.Project" displayId="PROJECTPATH">
              </Item>
            </Field>
            <Field name="configpath">
              <Item id="CONFIGPATH" modelType="si.Project" displayId="CONFIGPATH">
              </Item>
            </Field>
            <Field name="location">
              <Value dataType="string">CONFIGPATH/efils.c</Value>
            </Field>
            <Field name="variant">
              <Value></Value>
            </Field>
            <Field name="id">
              <Item id="775412:4" modelType="si.ChangePackage" displayId="775412:4">
              </Item>
            </Field>
            <Field name="summary">
              <Value dataType="string">teststring</Value>
            </Field>
            <Field name="server">
              <Item id="server.net" modelType="si.Server" displayId="server.net">
                <Field name="hostname">
                  <Value dataType="string">server.net</Value>
                </Field>
                <Field name="port">
                  <Value dataType="int">7001</Value>
                </Field>
              </Item>
            </Field>
            <Field name="linesadded">
              <Value dataType="long">281</Value>
            </Field>
            <Field name="linesdeleted">
              <Value dataType="long">278</Value>
            </Field>
            <Field name="bytesadded">
              <Value dataType="long">0</Value>
            </Field>
            <Field name="bytesdeleted">
              <Value dataType="long">0</Value>
            </Field>
            <Field name="istext">
              <Value dataType="boolean">true</Value>
            </Field>
          </Item>

[...]

1 个答案:

答案 0 :(得分:1)

我认为你需要这样做......

<xsl:call-template name="search-and-replace">
    <xsl:with-param name="input" select="Field[@name='configpath']/Item/@id"/>
    <xsl:with-param name="search-string" select="'STRINGTOSEARCHFOR'"/>
    <xsl:with-param name="replace-string" select="'STRINGTOREPLACE'"/>
</xsl:call-template>

请注意'STRINGTOSEARCHFOR'(和'STRINGTOREPLACE')周围的撇号。如果没有这个,它们将被视为xpath表达式,因此它会尝试查找名为STRINGTOSEARCHFOR的元素,而不是使用文字字符串值。