.NET XSLT转换 - 优化 [英] .NET XSLT Transform - Optimization

查看:69
本文介绍了.NET XSLT转换 - 优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用.NET XSLT将XML转换为另一个XML文件。所有

这对于小文件来说都很好,但是当用大文件(30MB)进行测试时,它需要花费1小时到2小时来转换文件。


这是代码片段:


XPathDocument xdoc = new XPathDocument(fs);

XPathNavigator nav = xdoc.CreateNavigator( );

xslt.Transform(nav,null,strWriter,null);


我在这个论坛(Oleg T)中读到,使用键会加快

转型。除此之外,另一个建议是使用MSXML

哪个更快(我不能使用)。下面是输入XML,
XSLT和输出XML的示例。为了清楚起见,我截断了Input和

输出XML的一部分。有人可以给我建议

如何在XSLT中使用Keys或任何其他方法优化这个
转换?任何想法都会非常有用。


输入XML:

-------------------- -------------------------------------------------- -----------------------------------

<?xml version =" ; 1.0"编码= QUOT; UTF-8英寸?>

< POC xmlns =" www.poctest.com">


< Cars>

< West>

< row>

< Dept> 6000< / Dept>

< DeptDir> A< / DeptDir>

< Adny>分开< / Adny>

< Closd> 10/20 / 2003< / Closd>

< Open> 2/15/1996< / open>

< CarType> 0< / CarType>

< Desc1>总和1吹了它< / Desc1>

< FileNumber> 12352< / FileNumber>

< DevAmt> 0.00< / DevAmt>

< DevDate />

< StageAddr1> W。 117TH ST。< / StageAddr1>

< StageCity> ALL< / StageCity>

< StageCnty> County< / StageCnty>

< StageCode> RRS< / StageCode>

< StageSt> OH< / StageSt>

< SOrderNo> 656565< / SOrderNo>

< SPolDate> 10/4 / 1994< / SPolDate>

< Staff> WXW< / Staff>

< StBrdCompl />

< StBrdNumbr />

< BootComp> N< / BootComp>

< BootIns> Y< / BootIns>

< / row>


< row>

< Dept> 6001< / Dept>

< DeptDir> ; A< / DeptDir>

< Adny>分开< / Adny>

< Closd> 10/20 / 2003< / Closd>

< Open> 2/15/1996< / Open>

< CarType> 0< / CarType>

< Desc1>总和1吹了它< / Desc1>

< FileNumber> 12352< / FileNumber>

< DevAmt> 0.00< / DevAmt>
< DevDate />

< StageAddr1> W。 117TH ST。< / StageAddr1>

< StageCity> ALL< / StageCity>

< StageCnty> County< / StageCnty>

< StageCode> RRS< / StageCode>

< StageSt> OH< / StageSt>

< SOrderNo> 656565< / SOrderNo>

< SPolDate> 10/4 / 1994< / SPolDate>

< Staff> WXW< / Staff>

< StBrdCompl />

< StBrdNumbr />

< BootComp> N< / BootComp>

< BootIns> Y< / BootIns>

< / row>


< / West>


< South>

< row> ;

< Dept> 7000< / Dept>

< DeptDir> B< / DeptDir>

< Adny> apart< / Adny>

< Closd> 10/20 / 2003< / Closd>

< Open> 2/15/1996< / Open>

< CarType> 0< / CarType>

< Desc1>总和1吹了它< / Desc1>

< FileNumber> 12352< / File数字>

< DevAmt> 0.00< / DevAmt>

< DevDate />

< StageAddr1> 117TH ST。< ; / StageAddr1>

< StageCity> ALL< / StageCity>

< StageCnty> County< / StageCnty>

< StageCode> RRS< / StageCode>

< StageSt> OH< / StageSt>

< SOrderNo> 656ss565< / SOrderNo>

< SPOLDate> ; 10/4 / 1994< / SPolDate>

< Staff> WXW< / Staff>

< StBrdCompl />

< ; StBrdNumbr />

< BootComp> N< / BootComp>

< BootIns> Y< / BootIns>

< /行>

< /南>

< / Cars>


< / POC>


------------------------------------------- -------------------------------------------------- ------------

XSLT:


<?xml version =" 1.0" encoding =" UTF-8"?>

< xsl:stylesheet xmlns:xsl =" http://www.w3.org/1999/XSL/Transform"

xmlns:poc =" www.poctest.com"

exclude-result-prefixes =" poc"

version =" 1.0"> ;

< xsl:output encoding =" UTF-8" />

< xsl:strip-space elements =" *" />

< xsl:template match =" /">

< xsl:apply-templates />

< / xsl:template>


< xsl:template match =" poc:Cars">

< Fields>

< xsl:apply-templates />

< / Fields>

< / xsl:template>


< xsl:template match =" poc:row">

<记录>

< LineNumber>< xsl:number level ="任何" />< / LineNumber>

< AreaPath>

< xsl:for-each select ="(ancestor :: *)">

< xsl:value-of select =" local-name()" />

< xsl:if test =" not(position() = last())"> \< / xsl:if>

< / xsl:for-each>

< / AreaPath>


< xsl:apply-templates select =" node()| @ *" />

< xsl:if test =" normalize-space(.// poc:Closd)=''''">

<状态> O< /状态>

< / xsl:if>

< xsl:if test =" normalize-space(.// poc:Closd) !=''''">

<状态> C< /状态>

< / xsl:if>

< / Record>

< / xsl:template>


< xsl:template match =" *">

< xsl:element name =" {name()}">

< xsl:apply-templates />

< / xsl:element>

< / xsl:template>


< xsl:template match =" poc:POC">

< xsl:apply-templates />

< / xsl:template>


< xsl:template match =" ; poc:West">

< xsl:apply-templates />

< / xsl:template>


< xsl:template match =" poc:East">

< xsl:apply-templates />

< / xsl :template>


< xsl:template match =" poc:South">

< xsl:apply-templates />

< / xsl:template>


< xsl:template match =" poc:North">

< xsl:apply-templates />

< / xsl:template>


<! - 用TermPost替换元素Adny - >

< xsl:template match =" poc:Adny">

< TermPost>

< xsl:apply-templates select =" @ * |节点()" />

< / TermPost>

< / xsl:template>

<! - 将元素FileNumber替换为TyreNumber - - >

< xsl:template match =" poc:FileNumber">

< TyreNumber>

< xsl: apply-templates select =" @ * |节点()" />

< / TyreNumber>

< / xsl:template>

<! - 用蝙蝠替换元素职员 - - >

< xsl:template match =" poc:Staff">

< Bat>

< xsl: apply-templates select =" @ * |节点()" />

< / bat>

< / xsl:template>

<! - 用日期替换元素打开 - - >

< xsl:template match =" poc:打开">

<>

< xsl: apply-templates select =" @ * |节点()" />

< / Date>

< / xsl:template>

<! - 将元素Closd替换为已关闭 - - >

< xsl:template match =" poc:Closd">

<已关闭>

< xsl: apply-templates select =" @ * |节点()" />

< / Closed>

< / xsl:template>


< / xsl:stylesheet>


-------------------------------------- -------------------------------------------------- -----------------

输出XML

<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>

<字段>

<记录>

< LineNumber> 1< / LineNumber>

< AreaPath> POC\Cars \ West< / AreaPath>

< Dept> 6000< / Dept>

< DeptDir> A< / DeptDir> ;

< TermPost>分开< / TermPost>

<已关闭> 10/20 / 2003< /已结束>

< Date> ; 2/15/1996< / Date>

< CarType> 0< / CarType>

< Desc1>总和1吹了它< / Desc1>

< TyreNumber> 12352< / TyreNumber>

< DevAmt> 0.00< / DevAmt>

< DevDate />

< StageAddr1> W。 117TH ST。< / StageAddr1>

< StageCity> ALL< / StageCity>

< StageCnty> County< / StageCnty>

< StageCode> RRS< / StageCode>

< StageSt> OH< / StageSt>

< SOrderNo> 656565< / SOrderNo>

< SPolDate> 10/4 / 1994< / SPolDate>

< Bat> WXW< / bat>

< StBrdCompl />

< StBrdNumbr />

< BootComp> N< / BootComp>

< BootIns> Y< / BootIns>

<状态> O< /状态>

< /记录>

< / Fields>

Hello all,
I am using .NET XSLT to transform an XML into another XML file. All
this is fine with small files, but when tested with big files (30MB) it
is taking between 1hr-2hrs to just transform the file.

Here is the code snippet:

XPathDocument xdoc = new XPathDocument(fs);
XPathNavigator nav = xdoc.CreateNavigator();
xslt.Transform(nav, null, strWriter, null);

I read in this forum (Oleg T) that using keys will speed up the
transformation. Apart from this the other suggestion was to use MSXML
which is much faster (Which I cannot use). Below are the Input XML,
XSLT and a sample of the Output XML. I truncated part of Input and
Output XML for clarity purposes. Can someone please give me suggestions
on how to use Keys in the XSLT or any other method of Optimizing this
tranformation? Any ideas will be really helpful.

Input XML:
---------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<POC xmlns="www.poctest.com">

<Cars>
<West>
<row>
<Dept>6000</Dept>
<DeptDir>A</DeptDir>
<Adny>apart</Adny>
<Closd>10/20/2003</Closd>
<Open>2/15/1996</Open>
<CarType>0</CarType>
<Desc1>Sum 1 blew it</Desc1>
<FileNumber>12352</FileNumber>
<DevAmt>0.00</DevAmt>
<DevDate />
<StageAddr1>W. 117TH ST.</StageAddr1>
<StageCity>ALL</StageCity>
<StageCnty>County</StageCnty>
<StageCode>RRS</StageCode>
<StageSt>OH</StageSt>
<SOrderNo>656565</SOrderNo>
<SPolDate>10/4/1994</SPolDate>
<Staff>WXW</Staff>
<StBrdCompl />
<StBrdNumbr />
<BootComp>N</BootComp>
<BootIns>Y</BootIns>
</row>

<row>
<Dept>6001</Dept>
<DeptDir>A</DeptDir>
<Adny>apart</Adny>
<Closd>10/20/2003</Closd>
<Open>2/15/1996</Open>
<CarType>0</CarType>
<Desc1>Sum 1 blew it</Desc1>
<FileNumber>12352</FileNumber>
<DevAmt>0.00</DevAmt>
<DevDate />
<StageAddr1>W. 117TH ST.</StageAddr1>
<StageCity>ALL</StageCity>
<StageCnty>County</StageCnty>
<StageCode>RRS</StageCode>
<StageSt>OH</StageSt>
<SOrderNo>656565</SOrderNo>
<SPolDate>10/4/1994</SPolDate>
<Staff>WXW</Staff>
<StBrdCompl />
<StBrdNumbr />
<BootComp>N</BootComp>
<BootIns>Y</BootIns>
</row>

</West>

<South>
<row>
<Dept>7000</Dept>
<DeptDir>B</DeptDir>
<Adny>apart</Adny>
<Closd>10/20/2003</Closd>
<Open>2/15/1996</Open>
<CarType>0</CarType>
<Desc1>Sum 1 blew it</Desc1>
<FileNumber>12352</FileNumber>
<DevAmt>0.00</DevAmt>
<DevDate />
<StageAddr1>117TH ST.</StageAddr1>
<StageCity>ALL</StageCity>
<StageCnty>County</StageCnty>
<StageCode>RRS</StageCode>
<StageSt>OH</StageSt>
<SOrderNo>656ss565</SOrderNo>
<SPolDate>10/4/1994</SPolDate>
<Staff>WXW</Staff>
<StBrdCompl />
<StBrdNumbr />
<BootComp>N</BootComp>
<BootIns>Y</BootIns>
</row>
</South>
</Cars>

</POC>

---------------------------------------------------------------------------------------------------------
XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:poc="www.poctest.com"
exclude-result-prefixes="poc"
version="1.0">
<xsl:output encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="poc:Cars">
<Fields>
<xsl:apply-templates/>
</Fields>
</xsl:template>

<xsl:template match="poc:row">
<Record>
<LineNumber><xsl:number level="any" /></LineNumber>
<AreaPath>
<xsl:for-each select="(ancestor::*)">
<xsl:value-of select="local-name()"/>
<xsl:if test="not(position()=last())">\</xsl:if>
</xsl:for-each>
</AreaPath>

<xsl:apply-templates select="node()|@*" />
<xsl:if test="normalize-space(.//poc:Closd)=''''">
<Status>O</Status>
</xsl:if>
<xsl:if test="normalize-space(.//poc:Closd)!=''''">
<Status>C</Status>
</xsl:if>
</Record>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="poc:POC">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="poc:West">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="poc:East">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="poc:South">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="poc:North">
<xsl:apply-templates/>
</xsl:template>

<!-- Replace Element Adny with TermPost -->
<xsl:template match="poc:Adny">
<TermPost>
<xsl:apply-templates select="@* | node()" />
</TermPost>
</xsl:template>
<!-- Replace Element FileNumber with TyreNumber -->
<xsl:template match="poc:FileNumber">
<TyreNumber>
<xsl:apply-templates select="@* | node()" />
</TyreNumber>
</xsl:template>
<!-- Replace Element Staff with Bat -->
<xsl:template match="poc:Staff">
<Bat>
<xsl:apply-templates select="@* | node()" />
</Bat>
</xsl:template>
<!-- Replace Element Open with Date -->
<xsl:template match="poc:Open">
<Date>
<xsl:apply-templates select="@* | node()" />
</Date>
</xsl:template>
<!-- Replace Element Closd with Closed -->
<xsl:template match="poc:Closd">
<Closed>
<xsl:apply-templates select="@* | node()" />
</Closed>
</xsl:template>

</xsl:stylesheet>

---------------------------------------------------------------------------------------------------------
Output XML

<?xml version="1.0" encoding="utf-8" ?>
<Fields>
<Record>
<LineNumber>1</LineNumber>
<AreaPath>POC\Cars\West</AreaPath>
<Dept>6000</Dept>
<DeptDir>A</DeptDir>
<TermPost>apart</TermPost>
<Closed>10/20/2003</Closed>
<Date>2/15/1996</Date>
<CarType>0</CarType>
<Desc1>Sum 1 blew it</Desc1>
<TyreNumber>12352</TyreNumber>
<DevAmt>0.00</DevAmt>
<DevDate />
<StageAddr1>W. 117TH ST.</StageAddr1>
<StageCity>ALL</StageCity>
<StageCnty>County</StageCnty>
<StageCode>RRS</StageCode>
<StageSt>OH</StageSt>
<SOrderNo>656565</SOrderNo>
<SPolDate>10/4/1994</SPolDate>
<Bat>WXW</Bat>
<StBrdCompl />
<StBrdNumbr />
<BootComp>N</BootComp>
<BootIns>Y</BootIns>
<Status>O</Status>
</Record>
</Fields>

推荐答案




das写道:



das wrote:


我正在使用。 NET XSLT将XML转换为另一个XML文件。所有

这对于小文件来说都很好,但是当用大文件(30MB)进行测试时,它需要花费1小时到2小时来转换文件。


这是代码片段:


XPathDocument xdoc = new XPathDocument(fs);

XPathNavigator nav = xdoc.CreateNavigator( );

xslt.Transform(nav,null,strWriter,null);
I am using .NET XSLT to transform an XML into another XML file. All
this is fine with small files, but when tested with big files (30MB) it
is taking between 1hr-2hrs to just transform the file.

Here is the code snippet:

XPathDocument xdoc = new XPathDocument(fs);
XPathNavigator nav = xdoc.CreateNavigator();
xslt.Transform(nav, null, strWriter, null);



什么是xslt,.NET 2.0 XslCompiledTransform或.NET 1.x

XslTransform?如果你使用.NET 1.x,你可以改为.NET 2.0和

XslCompiledTransform吗?


-


Martin Honnen --- MVP XML
http:// JavaScript .FAQTs.com /


" das" < Ad ******* @ gmail.comwrote in message

news:11 ********************** @ d34g2000cwd.googlegr oups.com ...
"das" <Ad*******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...

Hello all,

我使用.NET XSLT将XML转换为另一个XML文件。所有

这对于小文件来说都很好,但是当用大文件(30MB)进行测试时,它需要花费1小时到2小时来转换文件。


这是代码片段:


XPathDocument xdoc = new XPathDocument(fs);

XPathNavigator nav = xdoc.CreateNavigator( );

xslt.Transform(nav,null,strWriter,null);


我在这个论坛(Oleg T)中读到,使用键会加快

转型。除此之外,另一个建议是使用MSXML

哪个更快(我不能使用)。下面是输入XML,
XSLT和输出XML的示例。为了清楚起见,我截断了Input和

输出XML的一部分。有人可以给我建议

如何在XSLT中使用Keys或任何其他方法优化这个
转换?任何想法都会非常有用。
Hello all,
I am using .NET XSLT to transform an XML into another XML file. All
this is fine with small files, but when tested with big files (30MB) it
is taking between 1hr-2hrs to just transform the file.

Here is the code snippet:

XPathDocument xdoc = new XPathDocument(fs);
XPathNavigator nav = xdoc.CreateNavigator();
xslt.Transform(nav, null, strWriter, null);

I read in this forum (Oleg T) that using keys will speed up the
transformation. Apart from this the other suggestion was to use MSXML
which is much faster (Which I cannot use). Below are the Input XML,
XSLT and a sample of the Output XML. I truncated part of Input and
Output XML for clarity purposes. Can someone please give me suggestions
on how to use Keys in the XSLT or any other method of Optimizing this
tranformation? Any ideas will be really helpful.



假设你不能使用支持分析的几个工具之一

XSL转换,你应该这样做老式的方式优化它

与程序相同。特别是,你首先要在大部分时间里找到

,然后优化那一部分。


一个非常快捷的方法是尝试删除代码块,运行转换,并且

看看它加快了多少。我开玩笑地称这种技术为二进制销毁,

将其与二进制搜索进行对比。


1)删除一半代码

2)如果慢速部分在剩余代码中,则返回步骤1

3)如果缓慢部分在您删除的代码中,则将其放回,然后

4)删除你刚刚放回的一半代码并转到第2步


重复直到找到最慢的部分。


John

Assuming that you can''t use one of the several tools which support profiling
XSL transforms, you should do this the "old-fashioned way". Optimize it the
same way as you would a program. In particular, you''ll first want to find
out where most of the time is being spent, then optimize that section.

One very quick way is to try removing chunks of code, run the transform, and
see how much it is sped up. I jokingly call this technique "binary destroy",
to contrast it with "binary search".

1) Remove half the code
2) If the slow part is in the remaining code, then go back to step 1
3) If the slow part was in the code you removed, then put it back, then
4) Remove half of the code you just put back and go to step 2

Repeat until you find the slowest part.

John


感谢您的回复,

我会尝试您的方法,同时我还在寻找
" keys''方法。

是的,我不能将.NET 2.0用于XslCompiledTransform。

John Saunders写道:

Thanks for the reply,
I will try your approach, meanwhile I am still looking for the
"keys'' method in XSLT.
And yes I cannot use .NET 2.0 for XslCompiledTransform.
John Saunders wrote:


这篇关于.NET XSLT转换 - 优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆