2017-04-25 128 views
0

我有XML文檔這樣的:XSLT子元素

<OrdersSchedulePDFView> 
    <OrdersSchedulePDFViewRow>  
     <Items> 
     <ItemDesc>Content1</ItemDesc> 
     <ItemDesc>Content2</ItemDesc> 
     </Items> 
     <Locations> 
     <LocName>Content3</LocName> 
     <LocName>Content4</LocName> 
     </Locations> 
    </OrdersSchedulePDFViewRow> 
</OrdersSchedulePDFView> 

請給我一個例子XSL文件,在那裏我可以通過模板得到ItemDesc和LOCNAME元素。在此先感謝 這是我的xsl:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> 
<xsl:template match="OrdersSchedulePDFView"> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">   
     <xsl:for-each select="OrdersSchedulePDFViewRow">  
     <fo:page-sequence master-reference="all">   
      <fo:flow flow-name="xsl-region-body">        
       <xsl:template match="Locations"> 
        <xsl:copy> 
         <xsl:apply-templates select="LocName"/> 
        </xsl:copy> 
       </xsl:template>    
       <xsl:template match="Items">               
        <xsl:copy> 
         <xsl:apply-templates select="ItemDesc"/> 
        </xsl:copy> 
       </xsl:template>   
      </fo:flow> 
     </fo:page-sequence> 
     </xsl:for-each> 
    </fo:root> 
</xsl:template> 
</xsl:stylesheet> 
+0

你究竟在做什麼?你可以嘗試''(如果元素上沒有我們可能看不到的名稱空間),但模板應該做什麼? –

+0

我需要打印ItemDesc和LocName元素。但我無法通過讓他們的

+1

你爲什麼不發表您的嘗試,所以我們可以解決這個問題,而不是有從頭開始爲您編寫代碼。此外,請顯示您期望得到的**精確**輸出。 –

回答

0

雖然你的答案是相當模糊的,如果我沒有記錯的話,你需要的輸出:

<output> 
    <ItemDesc>Content1</ItemDesc> 
    <ItemDesc>Content2</ItemDesc> 
    <LocName>Content3</LocName> 
    <LocName>Content4</LocName> 
</output> 

第一種方法會映入腦海的使用遞歸模板:

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

<xsl:template match="OrdersSchedulePDFView"> 
    <output> 
     <xsl:apply-templates/> 
    </output> 
</xsl:template> 

<xsl:template match="ItemDesc"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

<xsl:template match="LocName"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

此迭代的每個節點,並且當一個匹配模板是發現執行copy-of

你在你的意見,你也想了for-each提及。這看起來像這樣:

<xsl:template match="/"> 
    <output> 
     <xsl:for-each select="//ItemDesc"> 
      <xsl:copy-of select="."/> 
     </xsl:for-each> 
     <xsl:for-each select="//LocName"> 
      <xsl:copy-of select="."/> 
     </xsl:for-each> 
    </output> 
</xsl:template> 
+0

當我嘗試申請 的 <的xsl:for-每個選擇= 「// ItemDesc」> 的 的 <的xsl:for-每個選擇= 「// LOCNAME」> 的 我得到: javax.xml.transform.TransformerException中:org.apache.fop.fo.ValidationException: 「FO:根」 缺少子元素。所需的內容模型:(layout-master-set,declarations?,bookmark-tree ?,(page-sequence | fox:external-document)+)(沒有可用的上下文信息) –