Copy node and alter its content

SnoWhite

I'm kinda stuck on a transformation i'd like to make on a xml file.

Basically i'm trying to copy all the xml but change some tags which only begins like this

XML code :

<test alt="foo" title="bar"/>

What i'd like to get after passing the xsl :

<test alt="foo"/>

Or

<change alt="foo" title=""/>

Thing is, sometimes i got tag with a lot of attributes, so i dont want to make a template match and then change every attributes manually.

Actually i'm doing this :

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

<xsl:template match="test/@title">
    <xsl:attribute name="title">
        <xsl:value-of select=""/>
    </xsl:attribute>
</xsl:template>

But it doesnt change the content of title in the output.

Martin Honnen

For all such tasks you should start with the identity transformation template

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

and then add templates for nodes that need special treatment, for instance

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

<xsl:template match="test/@title"/>

would copy everything unchanged but would delete all title attributes of test elements.

Or

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

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

<xsl:template match="test/@title">
    <xsl:attribute name="title"/>
</xsl:template>

should implement your second requirement. If you still have problems then post minimal but complete samples allowing us to reproduce the problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

puppet copy directory but not its content

From Dev

is there any possibility to alter an array element by modifying its copy?

From Dev

Copy recursively a directory excluding a subdirectory and ITS CONTENT

From Dev

How to copy formula without changing its content?

From Dev

Robocopy copy directory instead of just its content

From Dev

Copy CSS file content node js

From Dev

copy and alter column in CSV

From Dev

Make innosetup copy the full folder instead of only its content

From Dev

How to copy a cloned node in an iframe to its parent with styles?

From Dev

Using PHP to fetch content from a DOM node but not from its children

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

Using javascript to alter content of a <div>

From Dev

How to copy content of one text file to another text file with only filename and its extension using PowerShell?

From Dev

targeting an element by its content and the content of its ancestors

From Dev

Comparing a float and its copy

From Dev

Comparing a float and its copy

From Dev

How to alter content generated by Veiws module?

From Dev

Prevent wp_editor to alter initial content

From Dev

TextView will not center its content

From Dev

delete a folder and its content

From Dev

if condition changed by its content

From Dev

Relativelayout not wrapping its content

From Dev

Summernote will not edit its content

From Dev

Cloning an array with its content

From Dev

TextView will not center its content

From Dev

Get cell by its content

From Dev

Aggregate vector by its content

From Dev

Javascript copy content to clipboard

Related Related

HotTag

Archive