XSLT替换重复文本

时间:2014-04-15 13:12:04

标签: xml xslt

对于一个字段,我的文本会为我连接的每个节点重复一遍,就像这个

原始输出

;#{1220}Price:  ;#{1231}1x59 kr;#{1291}1x21 kr;#{1253}2x10 kr;#{1254}1x600 kr;#

价格在这里是根节点,后续的是子节点。我有兴趣从所有节点中删除;#{NODEID}和rootnode标题,以便输出:

通缉输出

1x59 kr, 1x21 kr, 2x10 kr, 1x600 kr

介意我想要;#从最后移除。

我已经实现了删除rootnode标题,但我不知道如何从所有节点中删除它。我应该补充一点,我对xslt很新。我使用以下命令删除rootnode标题:

<xsl:template name="FormatPrice" match="FieldRef[@Name='lookup']" mode="Lookup_body" >
<xsl:param name="lookupVal" />
<xsl:param name="clnVal"/>
<xsl:param name="index" select="0"/>

<xsl:variable name="concatinateClean">
  <xsl:choose>

    <xsl:when test="$clnVal =null or $clnVal ='' or $clnVal =' '">
      <xsl:value-of select="$clnVal"/>
    </xsl:when>
    <xsl:otherwise>

      <xsl:choose>
        <xsl:when test="contains($lookupVal,';#') and $index mod 2=1">
          <xsl:value-of select="concat($clnVal, '')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$clnVal"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:if test="contains($lookupVal,';#')">
  <xsl:variable name="firstHshRem" select="substring-after($lookupVal, ';#')"/>
  <xsl:variable name="afterSecondHash" select="substring-after($firstHshRem, ';#')" />
  <xsl:variable name="remNodeID" select="substring($afterSecondHash, 7)" />


  <xsl:variable name="tempClean">
    <xsl:choose>
      <xsl:when test="$index mod 2 = 1">
        <xsl:value-of select="$concatinateClean"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($concatinateClean,$remNodeID)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="temporary">
    <xsl:call-template name="FormatPrice">
      <xsl:with-param name="lookupVal" select="$firstHshRem"/>
      <xsl:with-param name="clnVal" select="$tempClean"/>
      <xsl:with-param name="index" select="$index+1"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="$temporary"/>
</xsl:if>
<xsl:if test="not(contains($lookupVal,';#'))">
  <xsl:value-of select="concat($concatinateClean,$lookupVal)"/>
  </xsl:if>
  </xsl:template>

非常感谢任何有关此方面的帮助

更新

感谢Marcus的帮助,并为我迟到的回复道歉。我现在越来越近了。

我在某种程度上编辑了您提供的代码,因为我使用的是Sharepoint,而我正在使用termstore-field进行搜索。我忘了提到我需要隐藏多个标题。我有一个Price标头和一个Location标头。

输出现在如下:

, Price:, 1x59 kr, 1x21 kr, 2x10 kr, 1x600 kr 
, Location:, Bermuda, Tenerife

使用以下代码:

<xsl:template name="FormatPL" match="FieldRef[@Name='lookup']" mode="Lookup_body" >
<xsl:param name="lookupVal" />

    <xsl:choose>

  <xsl:when test="contains($lookupVal, ';#{')">
    <xsl:value-of select="substring-before($lookupVal, ';#')"/>
    <xsl:text>, </xsl:text>
    <xsl:call-template name="FormatPL">
      <xsl:with-param name="lookupVal" select="substring-after(substring-after($lookupVal, ';#'), '}')"/>    
    </xsl:call-template>        
  </xsl:when>


  <xsl:when test="contains($lookupVal, ';#')">
    <xsl:value-of select="substring-before($lookupVal, ';#')"/>
  </xsl:when>

  <xsl:otherwise>
    <xsl:value-of select="$lookupVal"/>
  </xsl:otherwise>
</xsl:choose>

使用以下方式调用搜索:

 <td>
    <h3>
       Price:
    </h3>
 </td>
  <td>
    <p>
      <xsl:call-template name="FormatPL">
         <xsl:with-param name="lookupVal" select="@ows_PL" />
       </xsl:call-template>                
   </p>
 </td>
</tr>

其中@ows_PL是sharepoint中的字段,我对Location使用相同的字段。

我意识到这是基本的东西,但我很难做到这一点。我现在只需要在开始时删除“,Price:,”和“,Location:”。非常感谢任何进一步的帮助

1 个答案:

答案 0 :(得分:0)

假设您有一个输入XML,如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <price>;#{1220}Price:  ;#{1231}1x59 kr;#{1291}1x21 kr;#{1253}2x10 kr;#{1254}1x600 kr;#</price>
</root>

以下XSLT将生成所需的输出:

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

  <xsl:template match="price">
    <result>
      <xsl:call-template name="FormatPriceRecursive">
        <xsl:with-param name="text" select="substring-after(substring-after(text(), 'Price:  '), '}')"/>    
      </xsl:call-template>
    </result>
  </xsl:template>

  <xsl:template name="FormatPriceRecursive">
    <xsl:param name="text"/>

    <xsl:choose>

      <xsl:when test="contains($text, ';#{')">
        <xsl:value-of select="substring-before($text, ';#')"/>
        <xsl:text>, </xsl:text>
        <xsl:call-template name="FormatPriceRecursive">
          <xsl:with-param name="text" select="substring-after(substring-after($text, ';#'), '}')"/>    
        </xsl:call-template>        
      </xsl:when>

      <xsl:when test="contains($text, ';#')">
        <xsl:value-of select="substring-before($text, ';#')"/>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

</xsl:stylesheet>

请注意,我做了以下假设:

  • 可以忽略价格字符串前缀Price:
  • 一行始终由;#
  • 终止