saxon-he报错

1.最近在做java爬虫,使用的是webmagic框架,为了使用xpath2.0的语法引入了webmagic-saxon的依赖,导致出现报错:

           java.lang.RuntimeException: XPathFactory#newInstance() failed to.....Illegal config


2.找到报错的代码是webmagic-saxon引入的saxon-he中的配置文件报错

<dependency>
  <groupId>net.sf.saxon</groupId>
  <artifactId>Saxon-HE</artifactId>
  <version>9.5.1-1</version>
  </dependency>
3.原因:

google了很长时间终于找到答案

由于这个版本的saxon-he (9.5.1)中配置文件的格式不被jdk8接收。

JDK 6和JDK 7接受了非法复合格式;;JDK 8不再接受这种格式。

而我使用的正式jdk1.8

4.解决方案

使用saxon-he的最新版本

就我使用webmagic-saxon而言,首先在webmagic-saxon中去除Saxon-HE,然后再添加Saxon-HE最新版本的依赖


<!-- 支持xpath2.0语法 -->
<dependency>
 <groupId>us.codecraft</groupId>
 <artifactId>webmagic-saxon</artifactId>
 <version>0.6.1</version>
 <exclusions>
  <exclusion>
   <groupId>net.sf.saxon</groupId>
   <artifactId>Saxon-HE</artifactId>
  </exclusion>
 </exclusions>

</dependency>




<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
 <groupId>net.sf.saxon</groupId>
 <artifactId>Saxon-HE</artifactId>
 <version>9.7.0-15</version>
</dependency>


这个网站可以搜dependency: http://mvnrepository.com/artifact/org.apache.httpcomponents




<?xml version=“1.0”encoding=“UTF-8”?><xsl:stylesheet version=“3.0”xmlns:xsl=“http:xmlns:fo=“http:xmlns:w=“http:exclude-result-prefixes=“w”><!--增强字体映射--><xsl:variable name="font-mapping"><font w:name="宋体"fo:name="SimSun"/><font w:name="黑体"fo:name="SimHei"/><font w:name="等线"fo:name="DengXian"/></xsl:variable><!--根模板--><xsl:template match="/"><fo:root><fo:layout-master-set><fo:simple-page-master master-name="A4"margin="1in"><fo:region-body margin-top="0.5in"margin-bottom="0.5in"/><fo:region-before extent="0.5in"/><fo:region-after extent="0.5in"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="A4"><fo:flow flow-name="xsl-region-body"><xsl:apply-templates select="//w:body/*"/></fo:flow></fo:page-sequence></fo:root></xsl:template><!--增强段落处理--><xsl:template match="w:p"><fo:block xsl:use-attribute-sets="paragraph-style"><xsl:apply-templates select="w:pPr/w:jc"mode="align"/><xsl:apply-templates select="w:pPr/w:ind"/><xsl:apply-templates select=".//w:r"/></fo:block></xsl:template><!--文本格式处理增强--><xsl:template match="w:r"><fo:inline><xsl:apply-templates select="w:rPr"/><xsl:value-of select="string-join(w:t, '')"/></fo:inline></xsl:template><!--增强字体处理--><xsl:template match="w:rPr"><xsl:variable name="w-font"select="w:rFonts/@w:ascii | w:rFonts/@w:hAnsi"/><xsl:attribute name="font-family"><xsl:value-of select="($font-mapping/font[@w:name = $w-font]/@fo:name, $w-font, 'SimSun')[1]"/></xsl:attribute><xsl:if test="w:sz/@w:val"><xsl:attribute name="font-size"select="w:sz/@w:val * 0.5 || 'pt'"/></xsl:if><xsl:if test="w:color/@w:val != 'auto'"><xsl:attribute name="color"select="'#' || w:color/@w:val"/></xsl:if><xsl:apply-templates select="w:b | w:i | w:u | w:strike"/></xsl:template><!--下划线和删除线处理--><xsl:template match="w:u"><xsl:attribute name="text-decoration">underline</xsl:attribute></xsl:template><xsl:template match="w:strike"><xsl:attribute name="text-decoration">line-through</xsl:attribute></xsl:template><!--表格处理增强--><xsl:template match="w:tbl"><fo:table table-layout="fixed"width="100%"><xsl:for-each select="w:tblGrid/w:gridCol"><fo:table-column column-width="{@w:w div 20}pt"/></xsl:for-each><fo:table-body><xsl:apply-templates select="w:tr"/></fo:table-body></fo:table></xsl:template><xsl:template match="w:tr"><fo:table-row><xsl:apply-templates select="w:tc"/></fo:table-row></xsl:template><xsl:template match="w:tc"><fo:table-cell border="1pt solid #000"padding="4pt"><fo:block><xsl:apply-templates select=".//w:p"/></fo:block></fo:table-cell></xsl:template><!--段落对齐处理--><xsl:template match="w:jc"mode="align"><xsl:attribute name="text-align"><xsl:choose><xsl:when test="@w:val = 'center'">center</xsl:when><xsl:when test="@w:val = 'right'">end</xsl:when><xsl:when test="@w:val = 'both'">justify</xsl:when><xsl:otherwise>start</xsl:otherwise></xsl:choose></xsl:attribute></xsl:template><!--段落缩进处理--><xsl:template match="w:ind"><xsl:attribute name="text-indent"><xsl:value-of select="@w:firstLine div 20 || 'pt'"/></xsl:attribute></xsl:template><!--增强段落样式--><xsl:attribute-set name="paragraph-style"><xsl:attribute name="space-after">12pt</xsl:attribute><xsl:attribute name="line-height">1.5</xsl:attribute><xsl:attribute name="text-align">left</xsl:attribute></xsl:attribute-set></xsl:stylesheet>这个不对,xsl:attribute没有select属性
最新发布
03-11
<think>好的,我现在需要帮助用户解决他们提供的XSLT样式表中的问题。用户指出“xsl:attribute没有select属性”,这确实是一个常见的错误。首先,我需要仔细检查用户提供的代码,找出具体哪里用到了`xsl:attribute`的`select`属性。 首先,我看到在字体大小处理的模板中,有这样一行: ```xml <xsl:attribute name="font-size" select="w:sz/@w:val * 0.5 || 'pt'"/> ``` 这里使用了`select`属性,但实际上`xsl:attribute`元素的内容应该通过子元素或文本来设置属性值,而不是使用`select`属性。正确的做法是使用`<xsl:value-of>`或者直接计算表达式,然后将其作为元素的内容。 接着在颜色处理部分: ```xml <xsl:attribute name="color" select="'#' || w:color/@w:val"/> ``` 同样的问题,这里错误地使用了`select`属性。应该移除`select`,并将表达式放入`<xsl:value-of>`中,或者直接拼接字符串作为属性值的内容。 另外,在段落缩进处理的模板中: ```xml <xsl:attribute name="text-indent" select="@w:firstLine div 20 || 'pt'"/> ``` 这里也错误地使用了`select`属性。正确的做法是计算值后,将其作为元素的内容,而不是使用`select`。 接下来,我需要回忆XSLT的正确语法。`xsl:attribute`元素的正确用法是通过其内容来生成属性值,而不是通过`select`属性。`select`属性通常用于如`xsl:value-of`或`xsl:apply-templates`等元素中,用于指定数据的来源。因此,用户代码中的这些`select`属性都是无效的,必须被移除,并将表达式转换为使用`<xsl:value-of>`或直接拼接字符串。 例如,字体大小的处理应该改为: ```xml <xsl:attribute name="font-size"> <xsl:value-of select="w:sz/@w:val * 0.5"/> <xsl:text>pt</xsl:text> </xsl:attribute> ``` 这里通过`xsl:value-of`计算数值,并添加单位“pt”。同样,颜色处理应该拼接`#`和颜色值,使用: ```xml <xsl:attribute name="color"> <xsl:text>#</xsl:text> <xsl:value-of select="w:color/@w:val"/> </xsl:attribute> ``` 对于段落缩进,正确的代码应该是: ```xml <xsl:attribute name="text-indent"> <xsl:value-of select="@w:firstLine div 20"/> <xsl:text>pt</xsl:text> </xsl:attribute> ``` 此外,还需要检查用户代码中是否有其他潜在问题。例如,在字体映射部分,变量`font-mapping`的定义是否正确,是否使用了正确的命名空间。还有,在表格处理中,列宽的计算是否正确,是否将`w:w`值除以20转换为pt单位,这可能涉及到Word的twips单位转换(1 twip = 1/20 pt)。 可能还需要检查属性值拼接的语法是否正确。例如,原来的`||`操作符在XSLT中并不存在,正确的字符串拼接应使用`concat()`函数或直接通过多个`xsl:value-of`和`xsl:text`元素组合。 总结需要修改的地方: 1. 移除所有`xsl:attribute`中的`select`属性。 2. 使用`xsl:value-of`和文本节点来生成属性值。 3. 确保字符串拼接正确,例如颜色值的`#`前缀。 4. 检查数值计算和单位添加是否正确。 最后,将这些修正点整理成用户容易理解的步骤,并给出修改后的代码示例,帮助用户正确修复他们的XSLT样式表。</think>您指出的问题正确,在XSLT中`xsl:attribute`确实没有`select`属性。以下是代码的修正步骤和说明: $\boxed{问题定位}$ 错误出现在以下三处使用了无效的`select`属性: 1. 字体大小处理:`<xsl:attribute name="font-size" select=...>` 2. 颜色值处理:`<xsl:attribute name="color" select=...>` 3. 段落缩进处理:`<xsl:attribute name="text-indent" select=...>` $\boxed{修正方案}$ 需改用以下方式构造属性值: 1. **字体大小处理**: ```xml <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size"> <xsl:value-of select="w:sz/@w:val * 0.5"/>pt </xsl:attribute> </xsl:if> ``` 2. **颜色处理**: ```xml <xsl:if test="w:color/@w:val != 'auto'"> <xsl:attribute name="color"> <xsl:text>#</xsl:text> <xsl:value-of select="w:color/@w:val"/> </xsl:attribute> </xsl:if> ``` 3. **段落缩进处理**: ```xml <xsl:template match="w:ind"> <xsl:attribute name="text-indent"> <xsl:value-of select="@w:firstLine div 20"/>pt </xsl:attribute> </xsl:template> ``` $\boxed{修正原理}$ 1. **XSLT语法规则**: - `xsl:attribute`的值应由其子元素内容生成,而非`select`属性 - 数值计算通过`xsl:value-of`实现 - 字符串拼接使用嵌套元素完成(如`xsl:text`) 2. **单位处理**: - Word的`w:sz`值以半磅为单位,乘0.5得实际磅值 - `w:firstLine`缩进值以twips为单位,需除以20转换为磅 $\boxed{改进建议}$ 1. **增加命名空间声明**: 原始代码缺少完整的命名空间声明,应补充: ```xml xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" ``` 2. **增强错误处理**: 建议在关键位置添加容错判断,例如: ```xml <xsl:attribute name="font-family"> <xsl:choose> <xsl:when test="$font-mapping/font[@w:name = $w-font]"> <xsl:value-of select="$font-mapping/font[@w:name = $w-font]/@fo:name"/> </xsl:when> <xsl:otherwise>SimSun</xsl:otherwise> </xsl:choose> </xsl:attribute> ``` $\boxed{修改后效果}$ 修正后的样式表将能正确: - 将12号字体转换为6pt - 将颜色值`FF0000`转换为`#FF0000` - 将首行缩进240 twips转换为12pt(240/20=12) - 避免XSLT处理器报错终止转换 建议使用XSLT 3.0处理器(如Saxon-HE 10+)并添加`xsl:mode on-no-match="shallow-copy"`以增强节点处理能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值