0

我是 XSLT 编程的新手,正在努力解决以下问题:

XML:

<All_Results>
<Result>
<url>http://server/sites/sitecoll/library/Folder/NewFolder/test v1.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>SomeValue1</HHUrl>
    </hithighlightedproperties>
<isdocument>True</isdocument>
<serverredirectedurl>SomeValue</serverredirectedurl>
</Result>
<Result>
<url>http://server/sites/sitecoll/library/NewFolder1/test v2.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>SomeValue1</HHUrl>
    </hithighlightedproperties>
<isdocument>True</isdocument>
<serverredirectedurl>SomeValue</serverredirectedurl>
</Result>
<Result>
<url>http://server/sites/sitecoll/library/NewFolder/test v1.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>SomeValue1</HHUrl>
    </hithighlightedproperties>
<isdocument>False</isdocument>
<serverredirectedurl>SomeValue1</serverredirectedurl>
</Result>
......
......

以下是要求:

对于每个“结果”部分,如果 (“isdocument” node = True),则读取“url”节点并在其值中获取 'library/' 之后的子字符串。从此输出中,获取最后一次出现“/”之前的子字符串。(使用单独的模板来实现)例如,对于第一个“Result”,它将是“Folder/NewFolder”。最后,在此输出之前和之后连接硬编码字符串,并将“HHUrl”和“ServerRedirectUrl”的值替换为“Results”下每个“Result”的最终输出。

输出

 <All_Results>
<Result>
<url>http://server/sites/sitecoll/library/Folder/NewFolder/test v1.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>http://SomeHardCodedString1/Folder/NewFolder/SomeHardCodedString2</HHUrl>
    </hithighlightedproperties>
<isdocument>True</isdocument>
<serverredirectedurl>
        http://SomeHardCodedString1/Folder/NewFolder/SomeHardCodedString2
    </serverredirectedurl>
</Result>
<Result>
<url>http://server/sites/sitecoll/library/NewFolder1/test v2.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>http://SomeHardCodedString1/NewFolder1/SomeHardCodedString2</HHUrl>
    </hithighlightedproperties>
<isdocument>True</isdocument>
<serverredirectedurl>http://SomeHardCodedString1/NewFolder1/SomeHardCodedString2
     </serverredirectedurl>
</Result>
<Result>
<url>http://server/sites/sitecoll/library/NewFolder/test v1.0.docx</url>
<hithighlightedproperties>        
      <HHUrl>SomeValue1</HHUrl>
    </hithighlightedproperties>
<isdocument>False</isdocument>
<serverredirectedurl>SomeValue1</serverredirectedurl>
</Result>
......
......

我已经修剪了原始 XML 输出以简化需求,并有一个与原始 XML 相关联的长而复杂的 XLST。目标是在“HHUrl”字符串呈现为 HTML 之前动态修改它。对于这个特殊要求,我创建并嵌入了以下代码,它部分工作:

<xsl:template name="stripLast">
    <xsl:param name="pText"/>
    <xsl:param name="pDelim" select="'/'"/>
     <xsl:if test="contains($pText, $pDelim)">
       <xsl:value-of select="substring-before($pText, $pDelim)"/>
       <xsl:if test="contains(substring-after($pText, $pDelim), $pDelim)">
         <xsl:value-of select="$pDelim"/>
       </xsl:if>
       <xsl:call-template name="stripLast">
         <xsl:with-param name="pText" select=
          "substring-after($pText, $pDelim)"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
       </xsl:call-template>
     </xsl:if>
   </xsl:template>  

<xsl:template match="@* | node()">
   <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>  

 <xsl:template match="All_Results/Result/hithighlightedproperties/HHUrl">  
     <xsl:param name="staticUrl" select=" 'https://SomeHardCodedString1/' "/> 
      <xsl:copy>  
      <xsl:variable name="urlValue" select="string(.)"/>
      <xsl:variable name="s" select="substring-after($urlValue, 'Portal/')"/>      
      <xsl:variable name="qsValue">
      <xsl:call-template name="stripLast">
        <xsl:with-param name="pText" select="$s"/>
      </xsl:call-template>
   </xsl:variable>
        <xsl:value-of select="concat($staticUrl, $qsValue, 'SomeHardCodedString2')"/>        
    </xsl:copy>
 </xsl:template>

任何帮助将不胜感激。

谢谢,

SharePoint 开发。

4

1 回答 1

0

我玩了一下,下面的想法可能有用。但是,如果您想要硬编码的位中有两个以上的级别(即“Folder/”、“Folder/Folder1/”、(如果它中断)、“Folder/ Folder1/Folder2/"),但你可以扩展这个想法:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="//HHUrl[ancestor::Result[isdocument[.='True']]]">
    <xsl:variable name="url" select="../../url"></xsl:variable>
    <xsl:variable name="firsttoken" select="concat(substring-before(substring-after($url,'library/'),'/'),'/')"></xsl:variable>
    <xsl:variable name="secondtoken" select="substring-before(substring-after($url,$firsttoken),'/')"></xsl:variable>
    <xsl:variable name="thirdtoken" select="concat($firsttoken,$secondtoken)"></xsl:variable>

    <HHUrl>http://SomeHardCodedString1/<xsl:if test="$secondtoken!=''"><xsl:value-of select="$thirdtoken"/></xsl:if><xsl:if test="$secondtoken=''"><xsl:value-of select="substring-before($firsttoken,'/')"/></xsl:if>/SomeHardCodedString2</HHUrl>

</xsl:template>

<xsl:template match="//serverredirectedurl[ancestor::Result[isdocument[.='True']]]">
    <xsl:variable name="url" select="../url"></xsl:variable>
    <xsl:variable name="firsttoken" select="concat(substring-before(substring-after($url,'library/'),'/'),'/')"></xsl:variable>
    <xsl:variable name="secondtoken" select="substring-before(substring-after($url,$firsttoken),'/')"></xsl:variable>
    <xsl:variable name="thirdtoken" select="concat($firsttoken,$secondtoken)"></xsl:variable>

    <serverredirectedurl>http://SomeHardCodedString1/<xsl:if test="$secondtoken!=''"><xsl:value-of select="$thirdtoken"/></xsl:if><xsl:if test="$secondtoken=''"><xsl:value-of select="substring-before($firsttoken,'/')"/></xsl:if>/SomeHardCodedString2</serverredirectedurl>

</xsl:template>
于 2013-09-26T02:54:22.357 回答