XSLT - 根据其他节点修改 XML 节点的值 [英] XSLT - Modifying value of a XML node depending on other nodes

查看:23
本文介绍了XSLT - 根据其他节点修改 XML 节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to XSLT programming and struggling with the following issue:

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

要求如下:

对于每个结果"部分,if ("isdocument" node = True),读取url"节点并在其值中的library/"之后获取子字符串.从这个输出,获取最后一次出现 '/' 之前的子字符串.(使用单独的模板来实现这一点)例如,对于第一个结果",它将是文件夹/新文件夹".最后,连接此输出前后的硬编码字符串并替换HHUrl"和ServerRedirectUrl"的值以及每个结果"的最终输出在结果"下.

For each "Result" section, if ("isdocument" node = True), read the "url" node and get substring after 'library/' in it's value. From this output, get the substring before the last occurrence of '/'. (Used separate a template to achieve this) For example, for the first "Result", it will be "Folder/NewFolder". Finally, concatenate hard-coded strings before and after this output and replace the values of "HHUrl" and "ServerRedirectUrl" with this final output for every "Result" under "Results".

输出

 <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 之前动态修改它.针对这一特殊要求,我创建并嵌入了以下代码,该代码部分有效:

I have trimmed the original XML output to simplify the requirement and have a long complex XLST associated with the original XML. The goal is to modify the "HHUrl" string on the fly before it is rendered as HTML. For this particular requirement, I have created and embedded the following code, which works partially:

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

任何帮助将不胜感激.

谢谢,

SharePointDev.

SharePointDev.

推荐答案

我有点玩,下面的想法可能有用.但是,如果要硬编码的位中有两个以上的级别(即文件夹/"、文件夹/文件夹 1/"、(如果它会中断)、文件夹/Folder1/Folder2/"),但你可以扩展这个想法:

I had a bit of a play around, the following idea might be useful. It will be a bit fragile though, if there are more than two levels in the bit you want to hard-code around (ie. "Folder/", "Folder/Folder1/", (and it breaks if), "Folder/Folder1/Folder2/"), but you could extend the idea:

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

这篇关于XSLT - 根据其他节点修改 XML 节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆