-2

Input XML

<root><table>
 <cell1>4324</cell1>
 <cell2>543</cell2>
</table>
<table1>
 <cell1>4324</cell1>
 <cell2>543</cell2>
</table1></root>

I am trying to copy my xml node table, so everything stays the same only values should be removed. I would need output with xslt2 to be

<root><table>
 <cell1></cell1>
 <cell2></cell2>
</table>
<table1>
 <cell1>4324</cell1>
 <cell2>543</cell2>
</table1></root>

Please note that above example is only to show what i am trying to do - basically emptying all table node values. I know how to remove/copy for example specific node cell1 with text(), but can not figure out how to empty whole node like shown in example.

Any help would be appreciated :D Thanks a lot, Eoglasi

eoglasi
  • 169
  • 1
  • 2
  • 10
  • Don't make a habit of asking trivial questions on Stackoverflow. Any effort on your part would be appreciated. If XSLT is so important to you, I suggest you spend a few minutes looking at the basics yourself. – Mathias Müller Feb 27 '14 at 15:08
  • Basically i tried, but did not manage to make it work - that is why i am asking. This question may seem trivial to you, but be sure that i would not ask it if i would know the answer. – eoglasi Feb 27 '14 at 15:32
  • I have corrected my initial question above for your reference. – eoglasi Feb 27 '14 at 15:44
  • You do not know the answer because you never really tried to understand XSLT, if I may say so. If you "basically tried", then you've already been informed many times that you should post this attempted code. – Mathias Müller Feb 27 '14 at 15:47
  • As i have written that i have tried with approach which did not work for me as parser returned error. Bassicly i now see what you have meant with attempted code - and will do so in near future strictly when asking questions here in stack overflow. – eoglasi Feb 27 '14 at 15:52

1 Answers1

1

Use an identity transform template to copy everything; use another template to suppress the text() nodes you don't want:

<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"/>

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

<xsl:template match="table/*/text()"/>

</xsl:stylesheet>

Applied to your modified input of:

<root>
    <table>
        <cell1>4324</cell1>
        <cell2>543</cell2>
    </table>
    <table1>
        <cell1>4324</cell1>
        <cell2>543</cell2>
    </table1>
</root>

The result is:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <table>
        <cell1/>
        <cell2/>
    </table>
    <table1>
        <cell1>4324</cell1>
        <cell2>543</cell2>
    </table1>
</root>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • This might work, but i see i fail to mention that i am targeting specific node. In my example table. I tried match="table/text()" with no success? Any idea how to make it work? – eoglasi Feb 27 '14 at 15:20
  • Cool, i was missing only /*/ :D This removes text, how would i approach if their is another node within for example cell1 and i would like to totally erase it? Now nodes within node cell1 i am emptying are untouched? – eoglasi Feb 27 '14 at 19:28
  • @eoglasi write an empty template for each node (or type of node) you want to exclude from the output. – michael.hor257k Feb 27 '14 at 19:51
  • Can that be done with above code, as i do want to write empty template for each node - i have around 500 different ones :D? – eoglasi Feb 28 '14 at 14:31
  • Sorry, I don't follow. If you have some kind of a **logic** that determines which nodes to exclude (or which ones to keep), then it can (probably) be implemented in XSLT. If you only have a **list**, then you will need to list them. I suggest you edit your question to make this point clear. – michael.hor257k Feb 28 '14 at 16:12
  • Thanks Michael i have confirmed your answer as it is correct. Regarding deleting specific nodes i will use this solution http://stackoverflow.com/questions/9385981/remove-elements-and-or-attributes-by-name-per-xsl-parameters – eoglasi Mar 01 '14 at 08:06