用户在XSL中定义的函数

时间:2015-07-17 06:22:01

标签: xml xslt

我正在尝试创建一个用户定义的函数" fetchXMLAttribute"在XSL中。现在我只是打印" abcd"在它里面。以下是我的XSL文件

ERROR:  'Could not compile stylesheet'FATAL ERROR:  'Error checking type of the expression 'funcall(fetchXMLAttribute, [])'.'
       :Error checking type of the expression 'funcall(fetchXMLAttribute, [])'.Exception in conversion javax.xml.transform.TransformerConfigurationException: Error checking type of the expression 'funcall(fetchXMLAttribute, [])'.

我已经尝试了一些例子,但没有一个工作。我发现一些命名空间用于用户定义的函数。即使我的函数写在同一个XSL文件中,我真的有必要吗?我收到这个错误:

Option Explicit
Sub PopulateNewNames()

Dim M As Integer, EndOfMaster As Integer, NameCounter As Integer
Dim Cell As Range
Dim NameValue As String
Dim NewNames() As Variant

EndOfMaster = ThisWorkbook.Worksheets("Employee Placement").Cells(Rows.Count, "A").End(xlUp).Row
Rank = ThisWorkbook.Worksheets("rank").Cells(Rows.Count, "A").End(xlUp).Row
NameCounter = 0
ReDim NewNames(NameCounter)

With Sheets("Employee Placement")
    For M = 2 To EndOfMaster
        If .Range("A" & M).Value <> "" Then
           NameValue = .Range("A" & M).Value
           Sheets("Rank").Activate
           With Sheets("Rank")
                .Columns("A:A").Select
                Set Cell = Selection.Find(What:=NameValue, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
           If Cell Is Nothing Then
                ReDim Preserve NewNames(NameCounter)
                NewNames(NameCounter) = Sheets("employee placement").Range("A" & M).Value
                NameCounter = NameCounter + 1
           End If
           End With
        End If
    Next M
End With

'Take results in Array and put them into the last cell of the rank spreadsheet
ThisWorkbook.Sheets("Rank").Range("A" & Rank + 1 & ":A" & UBound(NewNames) + 1) = WorksheetFunction.Transpose(NewNames)

End Sub

1 个答案:

答案 0 :(得分:2)

函数必须具有命名空间。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:my="http://tempuri.org/mynamespace"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xs my"
>
    <xsl:output method="xml" indent="yes" encoding="utf-8"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" />

    <xsl:function name="my:fetchXMLAttribute">
        <xsl:text>abcd</xsl:text>
    </xsl:function>

    <xsl:template match="/*">
        <html>
            <body>             
                <div xmlAttribute="{my:fetchXMLAttribute()}"> 
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

另请注意

  • 默认命名空间设置为XHTML
  • 名称空间xsmy将从输出中排除
  • <xsl:text>以避免不必要的空白
  • 属性值模板(attr="{xpath-expression}"