papeneri
papeneri

Reputation: 37

xslt transformation in java , attribute value not encoded

I am writing a java program where in some cases I have to perform a xslt transformation.

In this case i need to add an attribute named type at the level of the work. Its value should be the same as the value of the element ns2:type_work

For example:

<ns2:work>
     <ns2:type_work>PROP</ns2:type_work>
     <ns2:identifier_work>2017/375/943030</ns2:identifier_work>
<ns2:work>

should become

<ns2:work type="PROP">
     <ns2:type_work>PROP</ns2:type_work>
     <ns2:identifier_work>2017/375/943030</ns2:identifier_work>   
<ns2:work>

I have made the following 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>

and I apply it using the proper java functios (javax.xml.transform.), I get no erros, the attribute -type- is created but it is empty.

Does it have to do something with the XSLT version is my xslt not compatible with 1.0? How can I bypass this?

Upvotes: 0

Views: 157

Answers (1)

Tim C
Tim C

Reputation: 70598

If you are using XSLT 1.0, then the code needs to look like this, as select is not valid on xsl:attribute in XSLT 1.0

<xsl:attribute name="type">
   <xsl:value-of select="ns2:type_work/node()" />
</xsl:attribute>

(Note that you can just do <xsl:value-of select="ns2:type_work" /> here)

Better still, use Attribute Value Templates

<ns2:work type="{ns2:type_work}" />
   <xsl:apply-templates select="@*|child::node()" />
</ns2:work>

Upvotes: 1

Related Questions