嘿,我需要使用XML 1.0將每個<a>
標記中的內容修剪爲描述節點內的字符數限制20。下面是XML在HTML錨節點中修剪href
<description>
This is text <a href="http://stackoverflow.com/posts/14718323/edit">http://stackoverflow.com/posts/14718323/edit</a>.
Also here is more text than we have another
<a href="http://stackoverflow.com/posts/14718323/edit">http://stackoverflow.com/posts/14718323/edit</a>.
</description>
我需要它變成是這樣的:
<description>
This is text <a href="http://stackoverflow.com/posts/14718323/edit">http://stacko</a>.
Also here is more text than we have another
<a href="http://stackoverflow.com/posts/14718323/edit">http://stacko</a>.
</description>
我可以做大部分的邏輯,但我在做麻煩「的for-each」是搜索通過描述節點並轉換「每個」< a>。
這是否有意義?任何幫助讚賞。
**基於這裏提供的答案編輯13年2月7日*
是我在現在的位置。
<xsl:template match="/">
<xsl:apply-templates select="//description"/>
</xsl:template>
<xsl:template match="a">
<a href="{@href}">
<xsl:value-of select="substring(normalize-space(),1,20)"/>
</a>
</xsl:template>
的問題是「應用模板」不會起作用,因爲我有我的XSL多個模板。我需要專門調用一個模板,所以我認爲「呼叫模板」將成爲要走的路線。 「call-template」的唯一問題是我不知道如何指定一個特定的XML節點來引用。這裏是我到目前爲止已經破解它(deosn't工作):
<xsl:template match="/">
<xsl:call-template name="trim_text"/>
</xsl:template>
<xsl:template name="trim_text" match="//description">
<a href="{@href}">
<xsl:value-of select="substring(normalize-space(),1,20)"/>
</a>
</xsl:template>
最初的「調用模板」需要在一個<xsl:template match="/">
,因爲這是一個更大的功能去。所以,我需要三樣東西:
1)HREF留什麼在XML
2)<a>
標籤之間的文本被修剪爲20px
3)我需要調用一致這個模板來自一個更大的xsl模板,它在XML上做了很多轉換。這將是大約7個模板調用。
concatenate?輸出需要什麼樣子? – Vinit
我的歉意。基本上我需要將< a >標記中的字符限制爲20像素。因此,例如...「http://www.longlink/morelink/page/anotherpage」將不得不縮短爲「http://www.longlink/mor」 – DigitalMC
我認爲你的意思是「修剪」而不是連接 – Vinit