0

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?

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I suspect the stylesheet says version="2.0" and is being run with a 1.0 processor. A 1.0 processor given a stylesheet that says version="2.0" is supposed to ignore all attributes it doesn't understand, which would account for the absence of any error here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.