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

November 3rd, 2010, 04:26 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Fetching variable from an XSLT transformation
Hi,
I have an XML document which I transform using a list of XSLT stylesheets.
Is there anyway to fetch out of these transformation the content of a variable which was calculated in a transformation?
In other words, I know we can pass variables to stylesheet transformation, but is it possible get results from a stylesheet transformation without having to parse the XML output?
|
|

November 3rd, 2010, 04:28 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Not directly with XSLT which has limited input/output facilities, similar in a way to something like JavaScript.
Usually that is taken care of by the process you are using to execute the transformation. What is your current scenario?
|
|

November 3rd, 2010, 04:35 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The output of XSLT is always one or more XML documents.
If you want other things out (for example an integer or a sequence of dates) then you might consider using XQuery.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

November 3rd, 2010, 05:04 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your replies.
Basically I do something similar to this.
I have a list of Transformer which pipe its result to the next one. I have implemented it similarly to this:
Code:
SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();
// These templates objects could be reused and obtained from elsewhere.
Templates templates1 = stf.newTemplates(new StreamSource(
getClass().getResourceAsStream("MyStylesheet1.xslt")));
Templates templates2 = stf.newTemplates(new StreamSource(
getClass().getResourceAsStream("MyStylesheet2.xslt")));
TransformerHandler th1 = stf.newTransformerHandler(templates1);
TransformerHandler th2 = stf.newTransformerHandler(templates2);
th1.setResult(new SAXResult(th2));
th2.setResult(new StreamResult(System.out));
Transformer t = stf.newTransformer();
t.transform(new StreamSource(System.in), new SAXResult(th1));
// th1 feeds th2, which in turn feeds System.out.
Typically the last transformation will be to create an HTML output. One of the intermediate transformation will make some complicated calculation which I would like to extract. Also I would not like to recalculate it or having to embed it in the last transformation result, or having to parse an intermediate result.
Is there a way for example to fetch from Xalan one variable or a state which had been set while doing these transformations?
|
|

November 3rd, 2010, 05:15 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
The output of XSLT is always one or more XML documents.
|
Are you saying that from one XSLT transformation we could generate two XML output files? If so could you please lead me to an example which could do this? Are you referring to <xsl:result-document/>? I am using XSLT 1.0! Can I get this to work with XSLT 1.0?
|
|

November 3rd, 2010, 05:50 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If you're using Java, then there is no possible reason for still using XSLT 1.0 - you're working with one hand tied behind your back.
Yes, you can use xsl:result-document to produce multiple outputs. If you're using Saxon you can also use the Java API to direct the output of xsl:result-document to your Java application.
I hesitate to say that this is the right way for you to go however. The questions you are asking suggest to me that I would really want to look hard at the top-level design of your application architecture. However, it's very hard to do that without getting deep into understanding the requirements, which can't really be done via this kind of forum.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

November 3rd, 2010, 09:34 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks,
I will look in all of this.
|
|

December 2nd, 2010, 06:21 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I solved this using the technique I use in Passing normal Java Objects to Transformer.setParameters
- I pass an object to the transformer.setParameters.
- Then my stylesheet calls some of my object's methods. It stores some of the status I need.
- When the transformation is finish I read these status from my object.
That's it. It avoids me re-parsing the big XML file.
|
|

December 8th, 2010, 08:45 AM
|
|
Authorized User
|
|
Join Date: Nov 2010
Posts: 50
Thanks: 0
Thanked 1 Time in 1 Post
|
|
How to eliminate empty element
Hi,
How to eliminate dummy element in xslt. Herewith i have pasted xml, xsl and html(The html file how it look like i inform the bottom)
XML Coding
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Preview-XML XREF b1.0.xsl"?>
<references>
<reference id="[1]">
<journal>
<journal_metadata>
<full_title></full_title>
<abbrev_title>Open Sys.</abbrev_title>
<issn></issn>
<coden></coden>
</journal_metadata>
<journal_issue>
<publication_date>
<month></month>
<day></day>
<year>2000</year>
</publication_date>
<journal_volume>
<volume>7</volume>
</journal_volume>
<issue></issue>
<special_numbering></special_numbering>
</journal_issue>
<journal_article>
<titles>bb
<title></title>
</titles>
<contributors>
<person_name sequence="first" contributor_role="author">
<given_name>V. P.</given_name>
<surname>BAlaji</surname>
<suffix></suffix>
</person_name>
<organization sequence="first|additional" contributor_role="author|editor|chair|translator"> </organization>
</contributors>
<publication_date>
<month></month>
<day></day>
<year></year>
</publication_date>
<pages>
<first_page>101</first_page>
<last_page></last_page>
<other_pages></other_pages>
</pages>
<doi_data>
<doi></doi>
<resource></resource>
</doi_data>
<comments>
<comment></comment>
</comments>
</journal_article>
<original_text>[1] V. P. BAlaji, Open Sys. <b>7</b>, 101 (2000).</original_text>
</journal>
</reference>
<references>
XSl Coding
<?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" indent="yes" />
<xsl:template match="node()">
<xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0]|@*)">
<xsl:copy>
<p><xsl:apply-templates select="@*|node()" /></p>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
Html Output
Open Sys. Information Dyn.
2000
7
bb
V. P.
Belavkin
101
[1] V. P. Belavkin, Open Sys. Information Dyn.7
, 101 (2000).
Excepted html output like (For the purpose before show the element name which element the contant text hold)
<abbrev_title> Open Sys.
<year> 2000
<volume> 7
<given_name> V. P.
<surname> BAlaji
<first_page> 101
[1] V. P. BAlaji, Open Sys. 7
, 101 (2000).
Kindly look at this.
|
|
 |