Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 8th, 2005, 05:58 PM
Registered User
 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath: String functions question.

I am trying to transform xml into c++ code. I want to put the source tree in c++ comments (/* ... */) so that people know where the c++ code was transformed from.

For example:
In xml:
<mylist>
   <item>foo</item>
   <item>bar</item>
</mylist>

In c++ code
/*
 Following code was created from xml source tree:
 <mylist>
   <item>foo</item>
   <item>bar</item>
 </mylist>
*/
std::vector<std::string> mylist;
mylist.push_back("foo");
mylist.push_back("bar");

However there is one problem bothering me. If in a xml source tree, any string contains substring "*/", it will break the c++ comment in c++ code.

Is there anyway I can recursively check node in the source tree to see if there is any string containing "*/"?

Thanks.


 
Old January 8th, 2005, 07:16 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Not exactly sure what your xslt is, but I'm assuming you're using the xsl:copy-of or xsl:copy to copy the xml, correct?

You could do a recursive copy, something like:
<xsl:copy>
  <xsl:apply-templates mode="copy"/>
</xsl:copy>
and whenever you hit a text node, check if it contains the string */ by using the contains function. eg.
<xsl:if test="contains(., '*/')">

 
Old January 8th, 2005, 08:01 PM
Registered User
 
Join Date: Jan 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, a simple version. Say I want to copy a source tree and put it between "/*" and "*/", only if the tree doesn't contain any node that has sub-string "*/".

The xslt is simply:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
/*
<xsl:copy-of select="."/>
*/
</xsl:template>
</xsl:stylesheet>

I want to be able to detect "*/" in the xml, output a warning message if there is any "*/".

I think the hard thing is doing the detection recursively. Because I can't assume any knowledge on the tree structure.

 
Old January 10th, 2005, 05:49 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How abouit using contains() to check the text value of the source tree?

e.g

xml:
<foo>
  <bar>something</bar>
  <bar>
     <comment>/* comment */</comment>
  </bar>
</foo>

xsl:
<xsl:template match="foo">
  <xsl:if test="contains(., '*/')">
    Warning - tree contains closing comment mark
  </xsl:if>
</xsl:template>

Regards
Bryan

 
Old January 10th, 2005, 03:00 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Quote:
quote:Originally posted by pupu79
<xsl:template match="/">
/*
<xsl:copy-of select="."/>
*/
</xsl:template>

I think the hard thing is doing the detection recursively. Because I can't assume any knowledge on the tree structure.
okay.
<xsl:template match="/">
/*
<xsl:copy>
  <xsl:apply-templates select="node()"/>
</xsl:copy>
*/
</xsl:template>

<xsl:template match="node()">
  <xsl:if test="contains(., '*/')>Warning, extra end comment in source</xsl:if>
  <xsl:copy>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

I'm not sure where you want your warning though.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath functions aldwinenriquez XML 4 August 25th, 2008 03:27 AM
Using string parameter as an XPath whelanm XSLT 3 August 15th, 2008 09:55 AM
Some XPath functions not available ole_v2 XSLT 4 March 29th, 2007 05:25 AM
Using Xpath functions Tomi XSLT 1 July 21st, 2006 05:43 AM
String functions..... BlueStag VB.NET 2002/2003 Basics 1 June 10th, 2005 05:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.