XSL值转字符串、Saxon删标签及xs命名空间报错问题咨询
XSLT相关问题解决方案
1 编译报错修复
你遇到的XPST0081 Namespace prefix 'xs' has not been declared报错和param的放置位置无关,你当前将param放在xsl:import之后、xsl:output之前的写法是符合XSLT语法规范的,报错原因是你使用了xs:string类型声明,但没有在根元素中声明XML Schema命名空间。
修复方法:在xsl:stylesheet标签中新增命名空间声明:xmlns:xs="http://www.w3.org/2001/XMLSchema"
另外你执行命令中存在参数错误:-xsl:role policy.xsl中间有空格,需要改为-xsl:role-policy.xsl,否则Saxon无法识别样式表文件路径。
2 XSL文档中的值转字符串的实现
XPath内置的string()函数可以将节点、数值等任意类型的取值转为字符串类型,你需要给取值包裹双引号的需求,有两种实现方案:
- 保留自定义quot参数:只要完成上述命名空间声明即可正常使用,拼接时建议给节点取值包一层
string()避免空节点报错,例如:$quot || string(role) || $quot - 无需额外定义参数:直接使用XML转义双引号
"拼接即可,例如:concat('"', string(role), '"')
3 解析后删除标签的方法
只需新增一个空的匹配模板即可,XSLT处理时命中该标签就会直接跳过,不会输出到结果中,模板写法如下:
<xsl:template match="dot-filename[normalize-space(text()) = 'graphs/node']"/>
将该模板放在样式表的顶层位置即可生效。
修正后的完整XSL代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.martin-loetzsch.de/DOTML" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0"> <xsl:import href="http://www.martin-loetzsch.de/DOTML/dotml2dot.xsl"/> <xsl:param name="quot" as="xs:string">"</xsl:param> <xsl:output method="text"/> <!-- 新增空模板删除指定dot-filename标签 --> <xsl:template match="dot-filename[normalize-space(text()) = 'graphs/node']"/> <xsl:template match="/"> <xsl:variable name="dotml"> <xsl:for-each select = "Import/Row"> <node style="filled" fontsize="16"> <xsl:attribute name="id"> <xsl:value-of select='$quot || string(role) || $quot'/> </xsl:attribute> <xsl:attribute name="label"> <xsl:value-of select='$quot || string(role) || $quot'/> </xsl:attribute> </node> <node style="filled" fontsize="16"> <xsl:attribute name="id"> <xsl:value-of select='$quot || string(items/subMenu[@name="pns"]/url) || $quot'/> </xsl:attribute> <xsl:attribute name="label"> <xsl:value-of select='$quot || string(items/subMenu[@name="pns"]/url) || $quot'/> </xsl:attribute> </node> <node style="filled" fontsize="16"> <xsl:attribute name="id"> <xsl:value-of select='$quot || string(items/subMenu[@name="pppk"]/url) || $quot'/> </xsl:attribute> <xsl:attribute name="label"> <xsl:value-of select='$quot || string(items/subMenu[@name="pppk"]/url) || $quot'/> </xsl:attribute> </node> <node style="filled" fontsize="16"> <xsl:attribute name="id"> <xsl:value-of select='$quot || string(items/subMenu[@name="ppt"]/url) || $quot'/> </xsl:attribute> <xsl:attribute name="label"> <xsl:value-of select='$quot || string(items/subMenu[@name="ppt"]/url) || $quot'/> </xsl:attribute> </node> <edge fontname="Arial" fontsize="9" label="Permit"> <xsl:attribute name="from"> <xsl:value-of select='$quot || string(role) || $quot'/> </xsl:attribute> <xsl:attribute name="to"> <xsl:value-of select='$quot || string(items/subMenu[@name="pns"]/url) || $quot'/> </xsl:attribute> </edge> <edge fontname="Arial" fontsize="9" label="Permit"> <xsl:attribute name="from"> <xsl:value-of select='$quot || string(role) || $quot'/> </xsl:attribute> <xsl:attribute name="to"> <xsl:value-of select='$quot || string(items/subMenu[@name="pppk"]/url) || $quot'/> </xsl:attribute> </edge> <edge fontname="Arial" fontsize="9" label="Permit"> <xsl:attribute name="from"> <xsl:value-of select='$quot || string(role) || $quot'/> </xsl:attribute> <xsl:attribute name="to"> <xsl:value-of select='$quot || string(items/subMenu[@name="ppt"]/url) || $quot'/> </xsl:attribute> </edge> </xsl:for-each> </xsl:variable> <xsl:apply-templates select="$dotml/node()"/> </xsl:template> </xsl:stylesheet>
内容的提问来源于stack exchange,提问作者Rafif Favian
相关产品推荐
相关产品推荐

