Discussion:
commenting an XML element [XSLT 1.0][xsltproc]
p***@thomsondigital.com
2010-08-18 10:52:57 UTC
Permalink
Hello all,

I am trying to comment one of the empty element (<spec/>) in my xml tree
but seems I am missing something.


My stylesheet look something below: <xsl:template match="spec"> <xsl:comment> <xsl:apply-templates select="self::*" mode="comment"/> </xsl:comment> </xsl:template> <xsl:template match="*" mode="comment"> <xsl:value-of select="'&lt;'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/> <xsl:apply-templates select="@*|node()" mode="comment" /> <xsl:value-of select="'&lt;/'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/>
</xsl:template>

<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>

This is what I get using xsltproc (MAC)

Error:
element text
misplaced text element

Any ideas please.

PS: I do have my identity transform defined in stylesheet.

TIA,
Pankaj

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2010-08-18 11:09:13 UTC
Permalink
Post by p***@thomsondigital.com
Hello all,
I am trying to comment one of the empty element (<spec/>) in my xml tree
but seems I am missing something.
</xsl:template>
<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>
This is what I get using xsltproc (MAC)
element text
misplaced text element
Any ideas please.
PS: I do have my identity transform defined in stylesheet.
I don't see any problem with that code, unless a spec element contains
"--". But in that case (XML input being

<root>
<spec foo="bar"><baz/></spec>
<spec>--</spec>
</root>

stylesheet being

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="spec"> <xsl:comment> <xsl:apply-templates select="self::*" mode="comment"/> </xsl:comment> </xsl:template> <xsl:template match="*" mode="comment"> <xsl:value-of select="'&lt;'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/> <xsl:apply-templates select="@*|node()" mode="comment" /> <xsl:value-of select="'&lt;/'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/>
</xsl:template>

<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>

</xsl:stylesheet>

) xsltproc (on Windows) gives a clear error message:

runtime error: file test2010081801Xsl.xml line 12 element comment
xsl:comment : '--' or ending '-' not allowed in comment

So try a different XSLT processor if you have a problem with xsltproc to
see whether another processor succeeds.

If you still have problems then consider to post complete samples
allowing us to reproduce.
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Wolfgang Laun
2010-08-18 11:23:54 UTC
Permalink
Shouldn't there be an fn:data() wrapping the '.' in <xsl:value-of
select="."/> for a text() node?
-W
Post by p***@thomsondigital.com
Hello all,
I am trying to comment one of the empty element (<spec/>) in my xml tree but seems I am missing something.
<xsl:template match="spec">
       <xsl:comment>
          <xsl:apply-templates select="self::*" mode="comment"/>
    </xsl:comment>
</xsl:template>
  <xsl:template match="*" mode="comment">
       <xsl:value-of select="'&lt;'"/>
           <xsl:value-of select="name()"/>
       <xsl:value-of select="'&lt;/'"/>
           <xsl:value-of select="name()"/>
   </xsl:template>
   <xsl:template match="text()" mode="comment">
       <xsl:value-of select="."/>
   </xsl:template>
       <xsl:value-of select="name()"/>
       <xsl:text>="</xsl:text>
       <xsl:value-of select="."/>
       <xsl:text>" </xsl:text>
   </xsl:template>
This is what I get using xsltproc (MAC)
element text
misplaced text element
Any ideas please.
PS: I do have my identity transform defined in stylesheet.
I don't see any problem with that code, unless a spec element contains "--". But in that case (XML input being
<root>
 <spec foo="bar"><baz/></spec>
 <spec>--</spec>
</root>
stylesheet being
<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 version="1.0">
   <xsl:copy>
   </xsl:copy>
 </xsl:template>
<xsl:template match="spec">
       <xsl:comment>
          <xsl:apply-templates select="self::*" mode="comment"/>
    </xsl:comment>
</xsl:template>
  <xsl:template match="*" mode="comment">
       <xsl:value-of select="'&lt;'"/>
           <xsl:value-of select="name()"/>
       <xsl:value-of select="'&lt;/'"/>
           <xsl:value-of select="name()"/>
   </xsl:template>
   <xsl:template match="text()" mode="comment">
       <xsl:value-of select="."/>
   </xsl:template>
       <xsl:value-of select="name()"/>
       <xsl:text>="</xsl:text>
       <xsl:value-of select="."/>
       <xsl:text>" </xsl:text>
   </xsl:template>
</xsl:stylesheet>
runtime error: file test2010081801Xsl.xml line 12 element comment
xsl:comment : '--' or ending '-' not allowed in comment
So try a different XSLT processor if you have a problem with xsltproc to see whether another processor succeeds.
If you still have problems then consider to post complete samples allowing us to reproduce.
--
       Martin Honnen
       http://msmvps.com/blogs/martin_honnen/
--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2010-08-18 11:34:50 UTC
Permalink
Post by Wolfgang Laun
Shouldn't there be an fn:data() wrapping the '.' in <xsl:value-of
select="."/> for a text() node?
xsltproc does XSLT 1.0 so fn:data does not even exist in that context.
And text nodes do not really have typed values anyway so I am not sure
why you suggest 'data' or why you think it helps.
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
David Carlisle
2010-08-18 11:31:23 UTC
Permalink
Post by Martin Honnen
I don't see any problem with that code
The code is trying to copy elements into a comment, but comments can not
conatin element nodes, they only have a text value, not children (the
same as attributes).

You need to serialise the element to a string (either using a template
usineg &lt;<xsl:value-of select="name()"/>.... or an extension such as
saxon:serialize().

David


________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2010-08-18 11:36:24 UTC
Permalink
Post by David Carlisle
Post by Martin Honnen
I don't see any problem with that code
The code is trying to copy elements into a comment, but comments can not
conatin element nodes, they only have a text value, not children (the
same as attributes).
You need to serialise the element to a string (either using a template
usineg &lt;<xsl:value-of select="name()"/>.... or an extension such as
saxon:serialize().
Are we talking about the same code? It does try to serialize with a
mode="comment": <xsl:template match="spec"> <xsl:comment> <xsl:apply-templates select="self::*" mode="comment"/> </xsl:comment> </xsl:template> <xsl:template match="*" mode="comment"> <xsl:value-of select="'&lt;'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/> <xsl:apply-templates select="@*|node()" mode="comment" /> <xsl:value-of select="'&lt;/'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/>
</xsl:template>

<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>

So where does that code try to put an element into a comment?
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
David Carlisle
2010-08-18 11:47:55 UTC
Permalink
Post by Martin Honnen
Are we talking about the same code?
yes but I think you read it better than me, sorry i'll go back to sleep
now:-)

David


________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-18 12:34:51 UTC
Permalink
Thanks everybody for your thoughts.
Martin, just for the brevity below is the snippet of my sample xml, which
is a table and <spec/> is actually is <colspec/> (copy pasted from the
wrong snippet earlier)

<?xml version="1.0" encoding="UTF-8"?>
<!-- New document created with EditiX at Wed Aug 18 17:37:11 IST 2010 -->
<table id="t0010" frame="topbot" rowsep="0" colsep="0">
<label>Table 3-1</label>
<caption>
<simple-para id="sp0200" role="title">This is a dummy title.</simple-para>
<simple-para id="sp0210">Ranjan</simple-para>
</caption>
<source>Source if present.</source>
<tgroup cols="2">
<colspec colname="col1"/>
<colspec colname="col2"/>
<thead valign="top">
<row>
<entry rowsep="1" align="left">Abbreviation</entry>
<entry rowsep="1" align="left">Dosage Interval</entry>
</row>
</thead>
<!-- More code here
-->
</tgroup>
</table>

Stylesheet snippet is same as posted earlier and I just tried using
"XALAN" in editix2010sp1 and got the following error.

Error:
==================================
No character data is allowed between top-level elements
No character data is allowed between top-level elements
No character data is allowed between top-level elements
No character data is allowed between top-level elements

Failed to compile stylesheet. 4 errors detected.

If I select the first error it highlights line
<entry rowsep="1" align="left">Dosage Interval</entry>

I am not sure but possibly my processing of entry element in stylesheet is
causing problem, which looks like below:

<xsl:template match="table//entry">
<xsl:copy>
<xsl:attribute name="table_type">cell</xsl:attribute>
<xsl:choose>
<xsl:when test="name(ancestor::*)='thead'">
<xsl:attribute name="theader"/>
<xsl:attribute
name="cellstyle">thead_cell</xsl:attribute>
<xsl:attribute name="valign">top</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>

</xsl:template>

Any thoughts please.

Please let me know if the snippets are not enough to understand the issue.

Best,
Pankaj


--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2010-08-18 12:41:01 UTC
Permalink
Post by p***@thomsondigital.com
Thanks everybody for your thoughts.
Martin, just for the brevity below is the snippet of my sample xml, which
is a table and <spec/> is actually is <colspec/> (copy pasted from the
wrong snippet earlier)
<?xml version="1.0" encoding="UTF-8"?>
<!-- New document created with EditiX at Wed Aug 18 17:37:11 IST 2010 -->
<table id="t0010" frame="topbot" rowsep="0" colsep="0">
<label>Table 3-1</label>
<caption>
<simple-para id="sp0200" role="title">This is a dummy title.</simple-para>
<simple-para id="sp0210">Ranjan</simple-para>
</caption>
<source>Source if present.</source>
<tgroup cols="2">
<colspec colname="col1"/>
<colspec colname="col2"/>
<thead valign="top">
<row>
<entry rowsep="1" align="left">Abbreviation</entry>
<entry rowsep="1" align="left">Dosage Interval</entry>
</row>
</thead>
<!-- More code here
-->
</tgroup>
</table>
Stylesheet snippet is same as posted earlier and I just tried using
"XALAN" in editix2010sp1 and got the following error.
==================================
No character data is allowed between top-level elements
No character data is allowed between top-level elements
No character data is allowed between top-level elements
No character data is allowed between top-level elements
Failed to compile stylesheet. 4 errors detected.
If I select the first error it highlights line
<entry rowsep="1" align="left">Dosage Interval</entry>
I don't know that tool but if it failed to compile the stylesheet then
it is rather odd that the error should occur in the source document,
there seem to be problems with the stylesheet itself, namely character
data other than white space between top level elements (i.e. between
xsl:template elements or other elements like xsl:output).
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-18 12:46:59 UTC
Permalink
Thanks again Martin for looking into it.

I tried testing it with below stylesheet, now complete one but with same
error. Something is terribly missing in code.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <!--Identity transform copies all items by default --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="colspec"> <xsl:comment> <xsl:apply-templates select="self::*" mode="comment" /> </xsl:comment> </xsl:template> <xsl:template match="*" mode="comment"> <xsl:value-of select="'&lt;'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/> <xsl:apply-templates select="@*|node()" mode="comment" /> <xsl:value-of select="'&lt;/'"/> <xsl:value-of select="name()"/> <xsl:value-of select="'&gt;'"/>
</xsl:template>

<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:template>

</xsl:stylesheet>

Just for reference, below is the sample xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- New document created with EditiX at Wed Aug 18 17:37:11 IST 2010 -->
<root>
<table id="t0010" frame="topbot" rowsep="0" colsep="0">
<label>Table 3-1</label>
<caption>
<simple-para id="sp0200" role="title">This is a dummy title.</simple-para>
<simple-para id="sp0210">Ranjan</simple-para>
</caption>
<source>Source if present.</source>
<tgroup cols="2">
<colspec colname="col1"/>
<colspec colname="col1"/>
<colspec colname="col2"/>
<thead valign="top">
<row>
<entry rowsep="1" align="left">Abbreviation</entry>
<entry rowsep="1" align="left">Dosage Interval</entry>
</row>
</thead>
<!-- More code here
-->
</tgroup>
</table>
</root>

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2010-08-18 13:02:10 UTC
Permalink
Post by p***@thomsondigital.com
I tried testing it with below stylesheet, now complete one but with same
error. Something is terribly missing in code.
I took your two code samples and run them successfully with both Saxon
6.5.5 and xsltproc, no errors were reported and the output contains

<!--<colspec>colname="col1" </colspec>-->
<!--<colspec>colname="col1" </colspec>-->
<!--<colspec>colname="col2" </colspec>-->

so the comments are created.
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-19 04:10:08 UTC
Permalink
Works for me now too. Yesterday was not my day. Little bit of tweaking in
stylesheet and I got the result the way I wanted (as an EMPTY element).

<!--<colspec colname="col1"/>-->
<!--<colspec colname="col2"/>-->

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <!--Identity transform copies all items by default --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Comment <colspec> element, which will require during export --> <xsl:template match="colspec"> <xsl:comment> <xsl:apply-templates select="self::*" mode="comment" /> </xsl:comment> </xsl:template> <xsl:template match="*" mode="comment"> <xsl:value-of select="'&lt;'"/> <xsl:value-of select="name()"/> <xsl:text> </xsl:text> <xsl:apply-templates select="@*|node()" mode="comment" /> <xsl:value-of select="'&gt;'"/>
</xsl:template>

<xsl:template match="text()" mode="comment">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="@*" mode="comment">
<xsl:value-of select="name()"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
<xsl:text>/</xsl:text>
</xsl:template>
</xsl:stylesheet>

But what about <thead valign="top">, which contains child elements and I
wish to comment *only* <thead>.

<thead valign="top">
<row>
<entry rowsep="1" align="left">Abbreviation</entry>
<entry rowsep="1" align="left">Dosage Interval</entry>
</row>
</thead>

to

<!-- <thead valign="top"> -->
<row>
<entry rowsep="1" align="left">Abbreviation</entry>
<entry rowsep="1" align="left">Dosage Interval</entry>
</row>
<!--</thead> -->



<xsl:template match="thead">
<xsl:comment>
<xsl:apply-templates select="." mode="comment" />
</xsl:comment>
</xsl:template>


The above code does not seems to be working.

Secondly how about reverting *commented* element using stylesheet. Is
there a way around.
Can somebody throw some ideas.

Best,
Pankaj




--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
p***@thomsondigital.com
2010-08-19 12:37:42 UTC
Permalink
Never mind, I will try to reverting comments back to original nodes.

Something like below.

xsl:template match="comment()">
<xsl:value-of select="."/>
</xsl:template>

Thanks for reading.


--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--

Florent Georges
2010-08-18 19:21:43 UTC
Permalink
Post by David Carlisle
You need to serialise the element to a string (either using
a template usineg &lt;<xsl:value-of select="name()"/>.... or an
extension such as saxon:serialize().
...or a serializer written in plain XSLT:

http://code.google.com/p/xlibs/source/browse/serial/trunk

Regards,
--
Florent Georges
http://fgeorges.org/






--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Loading...