2016-03-27 68 views
0

在HTML我有一個鏈接看起來像:XSLT - 得到lat和長從谷歌地圖網址

<a href="http://maps.apple.com/?ll=-23.590877,-46.689397&address=JK%20Iguatemi%20Avenida%20Presidente%20Juscelino%20Kubitschek,%202041,%20Itaim%20Bibi,%20São%20Paulo%20-%20SP,%2004543-011,%20Brésil" class="setItineraire">Discover</a> 

我怎樣才能把從該網址lat和長(-23.590877和-46.689397),併爲它們分配元素

<latitude><xsl:value-of select="" /></latitude> 
<longitude><xsl:value-of select="" /></longitude> 

這是我第一次面對這種情況。

感謝

+1

一個例子不建立規則有點事情簡單化。 –

+1

此外,輸入不是XML(包含未轉義的&符號)。 –

+0

是的,[你應該逃避你的URI](https://unspecified.wordpress.com/2011/06/02/you-should-xml-escape-your-uris/)。如果您的XSLT處理器不畏懼它,您可以查看XSLT字符串函數。 – usr2564301

回答

1

假設你的HTML也是良好的XML(這樣該&人物了逃脫的未解析文本&amp;,那麼如果你正在使用XSLT 1.0,您將需要一些字符串來做到這一點操縱。

首先從href屬性提取查詢字符串。

<xsl:variable name="qs" select="concat('&amp;', substring-after(@href, '?'), '&amp;')" /> 

注意,我加入了一個&到開始和字符串的結束麥e更容易地提取下一個參數。

要提取ll參數的值,那麼你這樣做(額外的&符號確保它只找到ll=1,2而不是all=1,2

<xsl:variable name="ll" select="substring-before(substring-after($qs, '&amp;ll='), '&amp;')" /> 

這應返回「-23.590877,-46.689397」,得到緯度(或經度)然後你可以只是這樣做

<latitude><xsl:value-of select="substring-before($ll, ',')" /></latitude> 

試試這個模板

<xsl:template match="a"> 
    <xsl:variable name="qs" select="concat('&amp;', substring-after(@href, '?'), '&amp;')" /> 
    <xsl:variable name="ll" select="substring-before(substring-after($qs, '&amp;ll='), '&amp;')" /> 
    <position> 
     <latitude><xsl:value-of select="substring-before($ll, ',')" /></latitude> 
     <longitude><xsl:value-of select="substring-after($ll, ',')" /></longitude>   
    </position> 
</xsl:template> 

請注意,如果你使用XSLT 2.0,您可以通過使用tokenize功能

<xsl:variable name="ll" select="tokenize(substring-after(@href, '?'), '&amp;')[starts-with(., 'll=')]" /> 
+0

謝謝你的回答 – Zeta