XSLT子元素

时间:2017-04-25 11:59:00

标签: apache xslt apache-fop

我有这样的xml文档:

<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>

1 个答案:

答案 0 :(得分: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>