2013-09-25 179 views
0

我是新來的XSLT編程,並與以下問題掙扎:XSLT - 依賴於其他節點XML節點的修改值

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 /'後獲取子字符串。從此輸出中, 在最後一次出現'/'之前獲取子字符串。 (使用單獨的模板 達到此目的)例如,對於第一個「結果」,它將是「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> 
...... 
...... 

我已修整的原始XML輸出,以簡化的要求,並且具有與原始XML相關聯的長複雜XLST。目標是在呈現爲HTML之前即時修改「HHUrl」字符串。對於這個特殊的要求,我已經創建並嵌入了下面的代碼,其中工程部分:

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

回答

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>