2012-07-03 75 views
1

交叉後http://perlmonks.org/index.pl?node_id=979710額外的XML標籤:XSLT創建文本文件

我試圖創建使用Perl和庫:: XSLT一些XML文本文件,我的改造工作正常,除了庫: :XSLT將一個不需要的?xml版本標籤添加到文件的開頭,如何阻止它執行此操作?

這裏是我的XSLT:

<xslt:stylesheet version="1.0" xmlns:data="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic" xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:message="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" omit-xml-declaration="yes"/> 
<xslt:param name="sep">|</xslt:param> 
<xslt:output method="text" /> 
<xslt:template match="message:MessageGroup"> 
<xslt:for-each select="data:DataSet"> 
<!-- get dimensions (but not time) and store in dimensions variable --> 
<xslt:for-each select="data:Series"> 
<xslt:variable name="dimensions"> 
<xslt:for-each select="data:SeriesKey"> 
<xslt:for-each select="data:Value"> 
<xslt:value-of select="@value" /> 
<xslt:value-of select="$sep" /> 
</xslt:for-each> 
</xslt:for-each> 
</xslt:variable> 
<!--get obs statuses and store in obs statuses variable--> 
<xslt:variable name="obsStatuses"> 
<xslt:for-each select="data:Attributes"> 
<xslt:for-each select="data:Value"> 
<xslt:value-of select="@value" /> 
</xslt:for-each> 
</xslt:for-each> 
</xslt:variable> 
<!--write out dimensions variable, time, observation, obsstatuses variable--> 
<xslt:for-each select="data:Obs"> 
<xslt:value-of select="$dimensions" /> 
<xslt:value-of select="data:Time" /> 
<xslt:value-of select="$sep" /> 
<xslt:value-of select="data:ObsValue/@value" /> 
<xslt:value-of select="$sep" /> 
<xslt:value-of select="data:Attributes/data:Value/@value"/> 
<xslt:text> 
</xslt:text> 
</xslt:for-each> 
</xslt:for-each> 
</xslt:for-each> 
</xslt:template> 
</xslt:stylesheet> 

這裏的Perl的:

use Lib::XSLT; 
my $parser = XML::LibXML->new(); 
my $xslt = XML::LibXSLT->new(); 
my $source = XML::LibXML->load_xml(location => "$xmlFile"); 
my $style_doc = $parser->parse_file(Path::Class::File->new("$xsltFile")); 
my $stylesheet = $xslt->parse_stylesheet($style_doc); 
open OUTPUTFILE, ">>$outputFile" or die("Unable to open $outputFile, $!"); 
print OUTPUTFILE $stylesheet->transform($source); 
close OUTPUTFILE; 
+0

請出示您的輸入和輸出。另外,它會讓你看到XSLT模板並避免所有嵌套for-eachs(在XSLT中最好避免)。 – Utkanos

回答

1

存儲$ stylesheet-結果>變換(),並使用$ stylesheet-> OUTPUT_FILE()修復了這個問題,e.g:

use Lib::XSLT; 
my $parser = XML::LibXML->new(); 
my $xslt = XML::LibXSLT->new(); 
my $source = XML::LibXML->load_xml(location => "$xmlFile"); 
my $style_doc = $parser->parse_file(Path::Class::File->new("$xsltFile")); 
my $stylesheet = $xslt->parse_stylesheet($style_doc); 
my $results = $stylesheet->transform($source); 
$stylesheet->output_file($results, $outputFile); 
0

爲什麼<?xml>聲明沒人要?它是有效的XML,並且對解析器沒有影響。

+0

我想要的輸出是用「|」分隔的純文本文件,而不是XML。 – Aeolai

+0

啊錯過了:)你有一個元素和一個元素。他們會衝突嗎? – Martin

+0

我試過刪除額外的標籤,但沒有幫助:o / – Aeolai