2016-11-24 64 views
0

我有一個Java對象,它是ArrayList和我已經在服務,這是在如下XML格式的響應增加其Java列表對象,如何閱讀的XSL

<root> 
<SubRoot> 
<type>A</type> 
<mand>Y</mand> 
<Section>B</Section> 
</SubRoot> 
<SubRoot> 
<type>A</type> 
<mand>Y</mand> 
<Section>A</Section> 
</SubRoot> 
</root> 

我試圖使用一些價值來自上面的xml條件。在真實條件的基礎上,我會爲我的目的調用一些模板。

我打電話下面我XSL模板,

<xsl:choose> 
     <xsl:when test="(/root/SubRoot[Section = 'A'])"> 
    //Call some template 
    </xsl:when> 
    <xsl:otherwise> 
    //some template 
</xsl:otherwise> 
</xsl:choose> 

<xsl:choose> 
    <xsl:when test="(/root/SubRoot[Section = 'B'])"> 
    //call some template 
    </xsl:when> 
<xsl:otherwise> 
//some template 
</xsl:otherwise> 
</xsl:choose> 

每次我的第一when將得到執行的不是第二when條件裏面來了。

你能幫我解釋一下這兩種情況如何得到執行嗎?在性能方面它是否正確?

任何建議方法必須讚賞。

+0

使用兩個'xsl:if test'而不是一個'xsl:choose/xsl:when'來解決這個問題。使用模板匹配可能會緩解XSLT的任務,但您尚未提供足夠的詳細信息。 –

+0

馬丁我也試過兩個如果和這個xsl代碼塊是在兩個獨立的xsl選擇。如果我將使用模板匹配會導致性能問題?我怎樣才能使用這與模板匹配。請您建議我。謝謝在高級 – Zia

+1

首先,我建議你發佈一個最低限度但有效的XSL版本,證明這是失敗的。例如:'when'節點被錯誤地關閉:'xsl'命名空間錯了。 –

回答

0

試試這個,

XSL

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://locomotive/bypass/docx"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" 
    indent="yes" /> 
<xsl:strip-space elements="*" /> 

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

<xsl:template match="SubRoot"> 
    <xsl:choose> 
     <xsl:when test="Section = 'A'"> 
      <!-- your logic for A --> 
      <A></A> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- your logic for B --> 
      <B></B> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

XML

<root> 
<SubRoot> 
    <type>A</type> 
    <mand>Y</mand> 
    <Section>B</Section> 
</SubRoot> 
<SubRoot> 
    <type>A</type> 
    <mand>Y</mand> 
    <Section>A</Section> 
</SubRoot> 

演示鏈接:http://xsltransform.net/ejivdHb/15