[英]Create custom tag extending XSLT using .NET
有沒有一種方法可以創建與XSLT相似的自定義標簽來擴展自定義功能?
即(在我的xslt文件中):
<xsl:template match="/">
<div>
<my:customTag items="3" classname="foo"/>
</div>
</xsl:template>
預期輸出:
<div>
<ul class="foo">
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<div>
目前,我正在這樣做:
<xsl:template match="/">
<div>
<xsl:copy-of select="my:customFunc(3,'foo')" />
</div>
</xsl:template>
和我的VB代碼中的customFunc做了這樣的事情:
Public Function customFunc(ByVal n As Integer, ByVal classname as String) As System.Xml.XmlNode
Dim newNode As System.Xml.XmlNode
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
Dim xmlContent As String = "<ul class=""" + classname + """>"
For v As Integer = 0 To n
xmlContent += "<li>" + someComplicatedCalc(n) + "</li>"
Next
xmlContent += "</ul>"
doc.LoadXml(xmlContent)
newNode = doc.DocumentElement
Return newNode
End Function
但是我想使用標簽代替函數。
我不知道此功能的任何支持,即使用Microsoft的XslCompiledTransform
和其他處理器(例如XmlPrime或Saxon( http://saxonica.com/html/documentation9.6/extensibility/instructions.html ))的自定義擴展元素的支持.NET似乎都支持它。
如果您正在尋找僅用XSLT替換VB函數的方法,則可以執行以下操作:
<xsl:template match="my:customTag">
<ul class="{@classname}">
<xsl:call-template name="expand_customTag">
<xsl:with-param name="i" select="1" />
<xsl:with-param name="count" select="@items" />
</xsl:call-template>
</ul>
</xsl:template>
<xsl:template name="expand_customTag">
<xsl:param name="i" />
<xsl:param name="count" />
<il>....</il>
<xsl:if test="$i < $count">
<xsl:call-template name="expand_customTag">
<xsl:with-param name="i" select="$i + 1" />
<xsl:with-param name="count" select="$count" />
</xsl:call-template>
</xsl:if>
</xsl:template>
這個想法是使用遞歸模板來生成您的<il>
元素,這將使您的XSLT更易於移植到其他XSLT處理器。
如果要使用現有的VB.Net代碼,但源XML中的語法更好,請嘗試將此模板添加到樣式表中。
<xsl:template match="my:customTag">
<xsl:copy-of select="my:customFunc(@items,@classname)" />
</xsl:template>
xpath選擇器將使用您的<my:customTag items="3" classname="foo"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.