Java中的XSLT转换,属性值未编码



我正在编写一个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,则代码需要如下所示,因为 XSLT 1.0 中的selectxsl:attribute 上无效

<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>

最新更新