2012-03-12 5 views
0

XML文書をCSVに変換して変換するXSLTがあります。しかし、読みにくく(私はすべての "../../"参照が嫌いです)、それらがパフォーマンスを傷つけるかどうか、私が変換しているファイルは大きいと思います。私はいくつかの異なる例を見てきましたが、私は下のものだけを動作させることができました。XML文書をCSVフラットファイルに変換するための、より効率的なXSLT

私の質問は次のとおりです。 1.このXSLTをより簡潔にし、 "../ .."参照を使用しないように書き換えることができますか? 2. "../ .."型参照は、何らかの形で値を格納するよりも効率が悪いです。

以下のサンプル。 おかげで、 ジョン


相続人XSLT:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 
<xsl:template match="/"> 
    <xsl:apply-templates select="Document"/> 
</xsl:template> 
<xsl:template match="Document"> 
    <xsl:apply-templates select="DocBody"/> 
</xsl:template> 
<xsl:template match="DocBody"> 
    <xsl:apply-templates select="Values"/> 
</xsl:template> 
<xsl:template match="Values"> 
    <xsl:apply-templates select="IntervalValues"/> 
</xsl:template> 
<xsl:template match="IntervalValues"> 
    <xsl:apply-templates select="Quantity"/> 
</xsl:template> 
<xsl:template match="Quantity"> 
    <xsl:value-of select="concat(../../../../DocHeader/DocTitle,',',../../../../DocHeader/CreatedAt,',',../../../DeliveryDate,',',../../../DeliveryHour,',',../Interval,',',Type,',',Value)"/> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

HERESに小さなXMLサンプル:

<?xml version="1.0" encoding="UTF-8"?> 
<Document> 
<DocHeader> 
<DocTitle>Totals Report</DocTitle> 
<DocRevision>1</DocRevision> 
<CreatedAt>2011-02-10T21:25:00</CreatedAt> 
</DocHeader> 
<DocBody> 
<DeliveryDate>2011-02-10</DeliveryDate> 
<DeliveryHour>22</DeliveryHour> 
<Values> 
<IntervalValues> 
<Interval>1</Interval> 
<Quantity> 
<Type>Energy</Type> 
<Value>18053.5</Value> 
</Quantity> 
<Quantity> 
<Type>Loss</Type> 
<Value>438.7</Value> 
</Quantity> 
<Quantity> 
<Type>Load</Type> 
<Value>17614.8</Value> 
</Quantity> 
</IntervalValues> 
<IntervalValues> 
<Interval>2</Interval> 
<Quantity> 
<Type>Energy</Type> 
<Value>17940.7</Value> 
</Quantity> 
<Quantity> 
<Type>Loss</Type> 
<Value>437.7</Value> 
</Quantity> 
<Quantity> 
<Type>Load</Type> 
<Value>17503</Value> 
</Quantity> 
</IntervalValues> 
<IntervalValues> 
<Interval>3</Interval> 
<Quantity> 
<Type>Energy</Type> 
<Value>17871.7</Value> 
</Quantity> 
<Quantity> 
<Type>Loss</Type> 
<Value>437.4</Value> 
</Quantity> 
<Quantity> 
<Type>Load</Type> 
<Value>17434.3</Value> 
</Quantity> 
</IntervalValues> 
</Values> 
</DocBody> 
</Document> 

HERESにサンプル出力

Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Energy,18053.5 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Loss,438.7 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,1,Load,17614.8 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Energy,17940.7 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Loss,437.7 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,2,Load,17503 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Energy,17871.7 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Loss,437.4 
Totals Report,2011-02-10T21:25:00,2011-02-10,22,3,Load,17434.3 

答えて

1

あなたは少し早い共通部分を引き出すことができ、このような何か:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="head"> 
<xsl:for-each select="Document/DocHeader"> 
    <xsl:value-of select="concat(DocTitle,',',CreatedAt,',')"/> 
</xsl:for-each> 
<xsl:for-each select="Document/DocBody"> 
    <xsl:value-of select="concat(DeliveryDate,',',DeliveryHour,',')"/> 
</xsl:for-each> 
</xsl:variable> 

<xsl:template match="/"> 
<xsl:apply-templates select="Document/DocBody/Values/IntervalValues/Quantity"/> 
</xsl:template> 

<xsl:template match="Quantity"> 
<xsl:value-of select="concat($head,../Interval,',',Type,',',Value)"/> 
<xsl:text>&#xA;</xsl:text> 
</xsl:template> 

</xsl:stylesheet> 
0

はあなたのために読み任意の容易なこのですか?

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:template match="/Document"> 
    <xsl:variable name="header" select="DocHeader"/> 
    <xsl:variable name="body" select="DocBody" /> 
    <xsl:for-each select="DocBody/Values"> 
     <xsl:for-each select="IntervalValues"> 
     <xsl:variable name="interval" select="Interval"/> 
     <xsl:for-each select="Quantity"> 
     <xsl:value-of select="$header/DocTitle"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="$header/CreatedAt"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="$body/DeliveryDate"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="$body/DeliveryHour"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="$interval"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="Type"/> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="Value"/> 
     <xsl:text>&#xA;</xsl:text> 
     </xsl:for-each> 
     </xsl:for-each> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
+0

ありがとうございました。私はこれらの答えに感謝します。 – John

関連する問題