0

I need to modify xml document with XSLT. what I need is to remove some node name from xml document.

Example:

<link ref="www.facebook.com">
       <c type="Hyperlink">www.facebook.com<c>
</link>

I need to convert this xml as follows (remove the <c> node and attribute form xml),

<link ref="www.facebook.com">
       www.facebook.com
</link>

I tried to do this in many ways but none of worked out well. any suggestions how can I do this ?

2 Answers 2

3

Here's one way:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="c">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Here's another:

<xsl:template match="link">
    <xsl:copy>
        <xsl:copy-of select="@* | c/text()"/>
    </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

1

And here's one more approach:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="link">
    <link>
    <xsl:copy-of select="@*"/><!-- copies the @ref attribute (and any other) -->    
    <xsl:value-of select="c[@type='hyperlink']"/>
    </link>    
</xsl:template>

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.