Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT-根据其他节点修改XML节点的值_Xslt_Xslt 1.0 - Fatal编程技术网

XSLT-根据其他节点修改XML节点的值

XSLT-根据其他节点修改XML节点的值,xslt,xslt-1.0,Xslt,Xslt 1.0,我是XSLT编程新手,正在努力解决以下问题: XML: <All_Results> <Result> <url>http://server/sites/sitecoll/library/Folder/NewFolder/test v1.0.docx</url> <hithighlightedproperties> <HHUrl>SomeValue1</HHUrl> </h

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

http://server/sites/sitecoll/library/Folder/NewFolder/test v1.0.docx
一些价值1
真的
一些价值
http://server/sites/sitecoll/library/NewFolder1/test v2.0.docx
一些价值1
真的
一些价值
http://server/sites/sitecoll/library/NewFolder/test v1.0.docx
一些价值1
假的
一些价值1
......
......

以下是要求:

<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”节点=True), 读取“url”节点并在其值中获取“library/”后面的子字符串。从这个输出中, 获取上次出现的“/”之前的子字符串。(用于分离模板以 实现这一点)例如,对于第一个“结果”,它将是“Folder/NewFolder”。 最后,在此输出前后串联硬编码字符串并替换 “HHUrl”和“ServerRedirectUrl”的值以及每个“结果”的最终输出 在“结果”下

输出

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

http://server/sites/sitecoll/library/Folder/NewFolder/test v1.0.docx
http://SomeHardCodedString1/Folder/NewFolder/SomeHardCodedString2
真的
http://SomeHardCodedString1/Folder/NewFolder/SomeHardCodedString2
http://server/sites/sitecoll/library/NewFolder1/test v2.0.docx
http://SomeHardCodedString1/NewFolder1/SomeHardCodedString2
真的
http://SomeHardCodedString1/NewFolder1/SomeHardCodedString2
http://server/sites/sitecoll/library/NewFolder/test v1.0.docx
一些价值1
假的
一些价值1
......
......

为了简化需求,我对原始XML输出进行了裁剪,并将一个长而复杂的XLST与原始XML关联起来。目标是在将“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>

我们将非常感谢您的帮助

谢谢


SharePointDev.

我有一个小游戏,下面的想法可能有用。不过,如果您想要硬编码的位中有两个以上的级别(即“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>

http://SomeHardCodedString1//SomeHardCodedString2
http://SomeHardCodedString1//SomeHardCodedString2