我是 XSL 的新手,我有一个带有表示 int rgb 颜色的标签的 xml 文件,我想使用 XSL 将它们转换为 Color 对象标签:
我的 xml 包含这些标签
<?xml version="1.0" encoding="UTF-8"?>
.
.
.
<ForeGroundColour>-16776961</ForeGroundColour>
.
.
.
</xml>
所需的 xml 是:
<ForeGroundColour>
<red>102</red>
<blue>102</blue>
<green>255</green>
<alpha>255</alpha>
</ForeGroundColour>
我的 XSL 文件是:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:Color="java.awt.Color"
xmlns:Integer="java.lang.Integer" exclude-result-prefixes="Color Integer ">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<!-- identity template !-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="//*[contains(name(),'ForegroundColour')]">
<xsl:copy>
<xsl:variable name="rgb" select="." />
<xsl:variable name="color" select="Color:new(Integer:parseInt($rgb))" />
<xsl:element name="red">
<xsl:value-of select="Color:getRed($color)" />
</xsl:element>
<xsl:element name="blue">
<xsl:value-of select="Color:getBlue($color)" />
</xsl:element>
<xsl:element name="green">
<xsl:value-of select="Color:getGreen($color)" />
</xsl:element>
<xsl:element name="alpha">
<xsl:value-of select="Color:getAlpha($color)" />
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我有两个问题:
1-当我运行此代码时,出现以下异常:
Exception in thread "main" java.lang.VerifyError: (class: transformer, method: template$dot$1 signature: (Lcom/sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;I)V) Expecting to find double on stack
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.getTransletInstance(TemplatesImpl.java:364)
at com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.newTransformer(TemplatesImpl.java:394)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:649)
at com.nyfix.ostp.server.workspacemigrator.XMLTransformer.transform(XMLTransformer.java:44)
at com.nyfix.ostp.server.workspacemigrator.XSLMigrator.migrate(XSLMigrator.java:45)
at com.nyfix.ostp.server.workspacemigrator.WorkspaceMigratorFactory.<init>(WorkspaceMigratorFactory.java:19)
at com.nyfix.ostp.server.workspacemigrator.WorkspaceMigratorFactory.main(WorkspaceMigratorFactory.java:45)
2-当我使用模板只是为了测试插入的节点时,
<xsl:template match="//*[contains(name(),'ForegroundColour')]">
<xsl:copy>
<xsl:variable name="rgb" select="." />
<xsl:element name="red">
<xsl:value-of select="$rgb" />
</xsl:element>
<xsl:element name="blue">
<xsl:value-of select="$rgb" />
</xsl:element>
<xsl:element name="green">
<xsl:value-of select="$rgb" />
</xsl:element>
<xsl:element name="alpha">
<xsl:value-of select="$rgb" />
</xsl:element>
</xsl:copy>
</xsl:template>
xml 在没有缩进的情况下进行转换:
<ForegroundColour>
<red>-16776961</red>
<blue>-16776961</blue>
<green>-16776961</green>
<alpha>-16776961</alpha>
</ForegroundColour>
谁能帮我这个?提前致谢