Link to home
Start Free TrialLog in
Avatar of praneetha
praneetha

asked on

how to read data in between comments

Hello,

Is there anyway that i can read what is in between <!-- and --> in xml file using XSL.

looks like its not possible you can write a comment but you cannot really read the comment from input xml.

may be we can like replace <!-- and --> with <comment> </comment> and then read it. any such ideas may be.

or i can use C# file classes to read the file and replace it. but i would prefer doing it using XSL than C# code.

Thanks

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

<xsl:value-of select="comment()"/>
gives you the content of the comment in a particular node

cheers

Geert
and here is a template that would copy comments from the source to the output (eg. in an identity transform)

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

cheers
Avatar of praneetha
praneetha

ASKER

ok how do i say if iam only interested in comments in between particular tags.

i tried this...but its not reading the comments. for some reason it doesnt work

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

so i tried this

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

this one reads every comment but i am only interested in comments in between <rec>

thanks i tried it like below but that didnt work either

<xsl:template match="rec/comment()">
    <xsl:comment>
      <xsl:value-of select="."/>
    </xsl:comment>
  </xsl:template>
<xsl:template match="rec">
    <marker text="just checking"/>
    <xsl:comment>
          <xsl:value-of select="comment()"/>
    </xsl:comment>
</xsl:template>

should copy the comment as it is
so I am wondering why your first test did not work.
It is the right way to find a comment inside an element
(note: it needs to be a direct child, nt a comment() in one of its children)

I added a <marker> element, for you to control that you actually caught the template for "rec"

cheers

Geert
it still doesnt work.

yes it is a direct children...

      <rec key="key1">
<!--
            <item id="24503" name="test reading between comments"
         </item>
  -->

</rec>
There must be something else wrong
(check modes, more specific templates,...)

with this XML
<?xml version="1.0" encoding="UTF-8"?>
     <rec key="key1">
         <!--
             <item id="24503" name="test reading between comments"
             </item>
         -->
     </rec>

and this XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="rec">
        <xsl:value-of select="comment()"/>
    </xsl:template>
</xsl:stylesheet>

I get this result with every single XSLT processor on my machine

<?xml version="1.0" encoding="UTF-8"?>
             &lt;item id="24503" name="test reading between comments"
             &lt;/item&gt;
         
please note that the tags inside the comment are meaningless... comments just take text
allthough you could make the tags meaningfull like this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="rec">
        <xsl:value-of select="comment()" disable-output-escaping="yes"/>
    </xsl:template>
</xsl:stylesheet>

cheers

Geert
it shouldnt matter if rec is embedded in other tags in xml. should it?

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok one of the parent tag contained xmlns link. when i removed that it works. but with it it doesnt work not sure why.

and if i want my output to contain < instead of &lt. can i do that
> one of the parent tag contained xmlns link.

well that is another issue
the default namespace is the xmlns given in the parent
so what you should do is this

consider the example
<?xml version="1.0" encoding="UTF-8"?>
<embed xmlns="whatever.your.namespace.is">
    <rec key="key1">
        <!--
            <item id="24503" name="test reading between comments"
            </item>
        -->
    </rec>
</embed>

now embed is in the declared namespace
and so is rec

here is the XSLT changed to take the namespace into account

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:ns1="whatever.your.namespace.is" exclude-result-prefixes="ns1">
    <xsl:template match="ns1:embed">
        <test><xsl:value-of select="ns1:rec/@key"/></test>
    </xsl:template>
    <xsl:template match="ns1:rec">
        <xsl:value-of select="comment()" disable-output-escaping="yes"/>
    </xsl:template>
</xsl:stylesheet>

- first I declare the namespaec with a prefix: xmlns:ns1="whatever.your.namespace.is"
- then I make sure this namespace doesn'tmake it to the output  exclude-result-prefixes="ns1"
- then I prefix all the elements according to the namespace they are in in the source document: <xsl:template match="ns1:rec">
- of course you need to replace ="whatever.your.namespace.is" by the actual namespace you are having

about the second part of your question
> and if i want my output to contain < instead of &lt. can i do that

yes, you can, by having the disable-output-escaping="yes" in the example
<xsl:value-of select="comment()" disable-output-escaping="yes"/>
doing so, you make sure that < is not replaced by the escaping mechanism into &lt;

cheers

Geert
thats cool.

but inspite of disable-output-escaping=yes. it still shows &lt instead of <
what is your output method?
XML or HTML?
it is XML.


  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
can you post the template where you do the disable-output escaping?

and show the actual comment it acts upon?

thanks

<varmap>

  <var>
    <rec key="key1">
      <!--
             <item id="24503" name="test reading between comments"
             </item>
         -->
    </rec>
  </var>
</varmap>

output

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8"/>
    <xsl:template match="rec">
      <xsl:value-of select="comment()" disable-output-escaping="yes"/>
    </xsl:template>
  </xsl:stylesheet>

i meant XSL not output
I am sorry,
this works nicely on all my processors
I don't have a clue what the problem might be

what processor are you using?
i am using vs.net 2005. so i guess msxml.

some how i couldnt get it to work. but its ok i will figure this out. thank u so much for your help :)
> some how i couldnt get it to work. but its ok i will figure this out. thank u so much for your help :)

I am sorry, I still don't know
I tested with Saxon in Oxygen IDE
I learned from your other question that you are downloading Saxon,
I would be interested to learn if you get it to work then

cheers

Geert