2017-04-25 149 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)+)(没有可用的上下文信息) –