2017-05-23 22 views
0

我是xslt中的新成員,並且必須將xml文件轉換爲html。我能夠檢索我的信息,但我想縮小一些字符串。我不知道如何調用xsl模板到我的值 - 選擇調用。有人能幫我嗎?替換選定值中的字符

編輯:我改變了我的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

我認爲你需要做的一個示例項.. 。

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

不是e圍繞'STRINGTOSEARCHFOR'(和'STRINGTOREPLACE')的撇號。如果沒有這個,他們將被視爲xpath表達式,因此它會嘗試查找名爲STRINGTOSEARCHFOR的元素,而不是使用文字字符串值。

+0

thx爲提示。但我認爲我的''調用存在一個主要問題。我沒有從我的XML獲得價值。如果我使用''我得到想要的字符串,但是我上面的代碼不工作... –

+0

對不起。忽略了第二行中的更改。但是,這也不工作.. –

+0

你能編輯你的問題,以顯示您的輸入XML請示例嗎?謝謝 –