2015-02-06 70 views
-1

我需要替換kml文件中的Iconstyle href。我遇到了麻煩以下書面權XSLT:XSLT:根據文本替換所有href


僞代碼:

選擇InconStyle 發現的所有HREF如果HREF |文本()= 「Y」,然後替換爲「 x「(其中y和x是映射列表

然後再次輸出整個文檔,並進行更改。


XML塊例如:

<Style id='sn_x_normal0'> 
    <IconStyle> 
     <color>FFFFFFFF</color> 
     <scale>0.75</scale> 
     **<Icon><href>/ge/icon1.gif</href></Icon>** 
     <hotSpot x='0.5' y='0' xunits='fraction' yunits='fraction'/> 
    </IconStyle> 
    <LineStyle> 
     <color>FFFFFFFF</color> 
    </LineStyle> 
    <PolyStyle> 
     <color>FFFFFFFF</color> 
    </PolyStyle> 
    <ListStyle> 
    </ListStyle> 
</Style> 
從上面的XML預期輸出

<Style id='sn_x_normal0'> 
    <IconStyle> 
     <color>FFFFFFFF</color> 
     <scale>0.75</scale> 
     **<Icon><href>/ge/icon2.gif</href></Icon>** 
     <hotSpot x='0.5' y='0' xunits='fraction' yunits='fraction'/> 
    </IconStyle> 
    <LineStyle> 
     <color>FFFFFFFF</color> 
    </LineStyle> 
    <PolyStyle> 
     <color>FFFFFFFF</color> 
    </PolyStyle> 
    <ListStyle> 
    </ListStyle> 
</Style> 

對XSLT嘗試:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="Document"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="Icon"> 
    <xsl:variable name="newIcon"> 
     <xsl:call-template name="string-replace-all"> 
     <xsl:with-param name="text" select="href" /> 
     <xsl:with-param name="replace" select="icon1.gif" /> 
     <xsl:with-param name="by" select="icon2.gif" /> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:for-each select="Icon/href"> 
     <xsl:value-of select="."/> 
    </xsl:for-each> 

    </xsl:template> 


    <xsl:template name="string-replace-all"> 
    <xsl:param name="text" /> 
    <xsl:param name="replace" /> 
    <xsl:param name="by" /> 
    <xsl:choose> 
     <xsl:when test="contains($text, $replace)"> 
     <xsl:value-of select="substring-before($text,$replace)" /> 
     <xsl:value-of select="$by" /> 
     <xsl:call-template name="string-replace-all"> 
      <xsl:with-param name="text" select="substring-after($text,$replace)" /> 
      <xsl:with-param name="replace" select="$replace" /> 
      <xsl:with-param name="by" select="$by" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 
+1

能否請您發表您的預期產出和嘗試XSLT。 – 2015-02-06 13:10:36

回答

2

選擇所有HREF缶第二在InconStyle如果HREF |文本()=「Y」,然後以「X」

這不正是你的例子告訴我們更換 。它「如果HREF |文()包含 」Y「 ...」

反正試試這樣說:

XSLT 1.0

<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:strip-space elements="*"/> 

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

<xsl:template match="Icon/href"> 
    <xsl:copy> 
     <xsl:call-template name="string-replace-all"> 
      <xsl:with-param name="text" select="." /> 
      <xsl:with-param name="replace" select="'icon1.gif'" /> 
      <xsl:with-param name="by" select="'icon2.gif'" /> 
     </xsl:call-template> 
    </xsl:copy> 
</xsl:template> 


<xsl:template name="string-replace-all"> 
    <xsl:param name="text" /> 
    <xsl:param name="replace" /> 
    <xsl:param name="by" /> 
    <xsl:choose> 
     <xsl:when test="contains($text, $replace)"> 
     <xsl:value-of select="substring-before($text,$replace)" /> 
     <xsl:value-of select="$by" /> 
     <xsl:call-template name="string-replace-all"> 
      <xsl:with-param name="text" select="substring-after($text,$replace)" /> 
      <xsl:with-param name="replace" select="$replace" /> 
      <xsl:with-param name="by" select="$by" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 

注單報價單位:

<xsl:with-param name="replace" select="'icon1.gif'" /> 
<xsl:with-param name="by" select="'icon2.gif'" /> 

如果沒有這些,您的模板將查找節點名爲icon1.gificon2.gif。在沒有這種節點的情況下,參數將是空的,並且模板將進入無限循環。


P.S.爲了使這更有效,改變:

<xsl:template match="Icon/href"> 

到:

<xsl:template match="Icon/href[contains(., 'icon1.gif')]"> 
+0

感謝您的回覆。對不起,因爲我不在辦公室,所以遲遲不能接受。這非常有幫助。 – Arnej65 2015-02-20 02:13:49