【发布时间】:2014-07-16 00:11:53
【问题描述】:
我正在尝试创建一个函数,它将我碰巧选择的任何文本都小写,无论是节点的纯文本值(“UserRole”)还是属性的值(“UserRoleInfo”)。这是示例数据:
<className>UserRole</className> <!-- Want to create text userRole -->
<fieldGroups>
<fieldGroup name="UserRoleInfo"/> <!-- Want to create text userRoleInfo -->
</fieldGroups>
如果您想提供解决方案,无需进一步阅读。但是,如果您有兴趣,这是我尝试实现这样的事情。
在这里,我选择类名:
<xsl:apply-templates mode="lowerCaseName" select="/className"/>
这里,我选择的是属性:
<xsl:apply-templates mode="printFieldGroupAssignments" select="/fieldGroups"/>
<xsl:template mode="printFieldGroupAssignments" match="fieldGroups">
<xsl:for-each select="./fieldGroup">
<xsl:apply-templates mode="lowerCaseName" select="./attribute::name"/>
</xsl:for-each>
</xsl:template>
所以这是我的“可重用函数”,它应该接受一个节点或一个属性。
<xsl:template mode="lowerCaseName" match="node()">
<xsl:value-of select="concat(lower-case(substring(.,1,1)), substring(.,2))"/>
</xsl:template>
<xsl:template mode="lowerCaseName" match="attribute()">
<xsl:value-of select="concat(lower-case(substring(.,1,1)), substring(.,2))"/>
</xsl:template>
我收到以下错误:
[ERROR]: 'attribute()' 中的语法错误。
[错误]:文件:Entity.xsl:第 849 行:解析 XPath 表达式“attribute()”时出错。
所以我把attribute()改为text()
<xsl:template mode="lowerCaseName" match="text()">
<xsl:value-of select="concat(lower-case(substring(.,1,1)), substring(.,2))"/>
</xsl:template>
我收到以下错误:
[错误]:无法编译样式表
[致命]:错误检查表达式'funcall(小写,[funcall(substring,[cast(variable-ref(entityClassName/node-set),string),cast(int-expr(1))的类型, real), cast(int-expr(1), real)])])'。
我认为我的问题是我不了解节点、节点集和纯旧文本之间的区别。
解决此问题的下一步应该是什么?我应该放弃并用不同的方法重新开始吗?欢迎所有建议。
【问题讨论】:
标签: xml xslt xpath xslt-2.0 xpath-2.0