0

There is this certain element in my xml:

<para><link href="D52871.dita">abc</link>
</para>

I want the output to be

<para><link id="D52871">abc</link>
</para>

I have used identity transform in the beginning to copy everything. I tried this code snippet

<xsl:template match="link/@href">
  <xsl:attribute name="id">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template> 

but it's not working probably because I need to specify that link element is inside para. Have tried few approaches to include that but none worked so far.

2
  • What do you mean by "not working"? What does your "identity transform" look like? It works for me. Also, do you need to change the href value from D52871.dita to D52871? Commented Jul 7, 2016 at 18:58
  • Does you actual XML have namespace at all (Look for xlmns="..." somewhere in it)? Commented Jul 7, 2016 at 19:02

1 Answer 1

1

The following works fine for me

<xsl:stylesheet  xmlns="http://www.w3.org/TR/xhtml1/strict" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"  version="1.0">
    <xsl:output method ="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="link/@href">
        <xsl:attribute name="id">
            <xsl:value-of select="substring-before(.,'.')"/>
        </xsl:attribute>
    </xsl:template> 

</xsl:stylesheet>

Input:

<?xml version="1.0" encoding="UTF-8"?>
<para>
   <link href="D52871.dita">abc</link>
</para>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<para>
   <link id="D52871">abc</link>
</para>
Sign up to request clarification or add additional context in comments.

Comments

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.