XSLT child elements

I have xml document like this:

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

Please, give me an example xsl file, where I can get ItemDesc and LocName elements via template. Thanks in advance This is my 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>

Upvotes: 0

Views: 1000

Answers (1)

D S
D S

Reputation: 31

Although your answer is rather vague, if I'm not mistaken you want the output:

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

The first method that would spring to mind is using recursive templates:

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

This iterates over each node, and when a matching template is found the copy-of is executed.

You mentioned in your comments that you would also like a for-each. This would looks something like this:

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

Upvotes: 1

Related Questions