2011-01-26 99 views
0

我遇到了xsl映射器的性能問題。 下面是一些例子XSL(注:真正的XSL這樣下去,10萬行)xsl性能問題

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns3="http://microsoft.com/HealthCare/HL7/2X/2.3.1/Tables" xmlns:ns4="http://microsoft.com/HealthCare/HL7/2X/2.3.1/DataTypes" xmlns:ns0="http://microsoft.com/HealthCare/HL7/2X/2.3.1/Segments" xmlns:ns2="http://microsoft.com/HealthCare/HL7/2X" xmlns:ns1="http://Cegeka.C2M.Accelerator.Schemas.segments_C2M"> 
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" /> 
<xsl:template match="/"> 
    <xsl:apply-templates select="/ns2:ADT_231_GLO_DEF" /> 
</xsl:template> 
<xsl:template match="/ns2:ADT_231_GLO_DEF"> 
<ns2:ADT_231_GLO_DEF> 
    <xsl:for-each select="EVN_EventType"> 
    <EVN_EventType> 
     <xsl:if test="normalize-space(EVN_1_EventTypeCode/text())"> 
     <EVN_1_EventTypeCode> 
      <xsl:value-of select="EVN_1_EventTypeCode/text()" /> 
     </EVN_1_EventTypeCode> 
     </xsl:if> 
     <EVN_2_RecordedDateTime> 
     <xsl:if test="normalize-space(EVN_2_RecordedDateTime/TS_0_TimeOfAnEvent/text())"> 
      <TS_0_TimeOfAnEvent> 
      <xsl:value-of select="EVN_2_RecordedDateTime/TS_0_TimeOfAnEvent/text()" /> 
      </TS_0_TimeOfAnEvent> 
     </xsl:if> 
     </EVN_2_RecordedDateTime> 
     <xsl:for-each select="EVN_3_DateTimePlannedEvent"> 
     <xsl:if test="normalize-space(TS_0_TimeOfAnEvent/text())"> 
      <EVN_3_DateTimePlannedEvent> 
      <TS_0_TimeOfAnEvent> 
       <xsl:value-of select="TS_0_TimeOfAnEvent/text()" /> 
      </TS_0_TimeOfAnEvent> 
      </EVN_3_DateTimePlannedEvent> 
     </xsl:if> 
     </xsl:for-each> 
     <xsl:if test="normalize-space(EVN_4_EventReasonCode/text())"> 
     <EVN_4_EventReasonCode> 
      <xsl:value-of select="EVN_4_EventReasonCode/text()" /> 
     </EVN_4_EventReasonCode> 
     </xsl:if> 
    </EVN_EventType> 
    </xsl:for-each> 
    </ns2:ADT_231_GLO_DEF> 
    </xsl:template> 
    </xsl:stylesheet> 

那麼,我做的是:

- I copy the nodes I want from the source xml 
- I don't copy the empty nodes or the nodes that contain a break (hence why I check normalize-space(/text()) 

現在的執行時間是約1秒,這是正常的嗎?我在biztalk中使用這種映射,通常每秒可以處理至少10條消息(如果不是更多:p),但是這個映射會造成延遲,所以我每秒只能處理1條消息:(

現在我是沒有一個xsl大師不幸的是,如果任何人可以給我一些建議,這是值得歡迎的:)

THX

+0

您不會說源郵件的大小是多少:這將是決定執行時間的主要因素。你也不會說你如何衡量執行時間 - 你是否包含XML解析時間?您通常可以通過確定執行時間是線性變化還是(比如說)以源文檔大小的二次方式獲得有用的診斷信息 - 也就是說,如果大小加倍,則經過時間會增加2倍或4倍或更差?如果它是二次的,那麼通常可以通過正確使用xsl:key來解決問題。 – 2011-01-26 10:43:50

+0

雖然沒有真正解決你的問題w.r.t.性能,還有一些'可愛的'遞歸XSLT模板匹配算法來一般地去掉空的節點 - 這可以爲你節省很多編碼嗎?例如http://www.stylusstudio.com/xsllist/200403/post50690.html – StuartLC 2011-01-26 10:56:45

回答

2

我複製節點,我從源XML要

我不要複製空節點或包含中斷的節點(因此我檢查歸一化空間)

第一個,我建議你可以使用帶有覆蓋的身份轉換。例如,下面的代碼將複製所有元素,但不包括那些「在空白(在空白標準化後)字符串值並且沒有子元素或屬性」的元素。

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

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

<xsl:template match="*[not(normalize-space()) and not(*) and not(@*)]"/> 

</xsl:stylesheet> 

,你可以嘗試,通過剝離在編譯時未使用空格:

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

這樣你的文件會保存在不顯着空格內存中,因此會更簡潔。