我正在编写一个java程序,在某些情况下我必须执行xslt转换。
在这种情况下,我需要在工作级别添加一个名为 type 的属性。 它的值应该与元素 ns2:type_work 的值相同
例如:
<ns2:work>
<ns2:type_work>PROP</ns2:type_work>
<ns2:identifier_work>2017/375/943030</ns2:identifier_work>
<ns2:work>
应该变成
<ns2:work type="PROP">
<ns2:type_work>PROP</ns2:type_work>
<ns2:identifier_work>2017/375/943030</ns2:identifier_work>
<ns2:work>
我制作了以下 XSLT
<xsl:template match="ns2:work">
<ns2:work>
<xsl:attribute name="type" select="ns2:type_work/node()" />
<xsl:apply-templates select="@*|child::node()" />
</ns2:work>
</xsl:template>
我使用正确的 java 函数 (javax.xml.transform.) 应用它, 我没有得到任何错误, 属性 -type- 已创建 但它是空的。
它是否与 XSLT 版本有关?我的 xslt 与 1.0 不兼容吗? 我怎样才能绕过这个?
最佳答案
如果您使用的是 XSLT 1.0,那么代码需要如下所示,如 select
在 xsl:attribute
上无效在 XSLT 1.0 中
<xsl:attribute name="type">
<xsl:value-of select="ns2:type_work/node()" />
</xsl:attribute>
(请注意,您可以在此处执行 <xsl:value-of select="ns2:type_work" />
)
更好的是,使用属性值模板
<ns2:work type="{ns2:type_work}" />
<xsl:apply-templates select="@*|child::node()" />
</ns2:work>
关于java - java中的xslt转换,属性值未编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46730624/