开发者

Extreme XSLT XML Flattening

I have the following input XML file:

<root>
 <a>
   <b>1</b>
 </a>
 <c>
 开发者_运维问答  <d>
     <e>2</e>
     <f>3</f> or <e>3</e>
   </d>
  <g h="4"/>
  <i>
    <j>
      <k>
        <l m="5" n="6" o="7" />
        <l m="8" n="9" o="0" />
      </k>
    </j>
  </i>
 </c>
</root>

I would like to use XSLT to transform it into the follow outputs:

OUTPUT 1

<root>
  <row b="1" e="2" f="3" h="4" m="5" n="6" o="7" />
  <row b="1" e="2" f="3" h="4" m="8" n="9" o="0" />
<root>

OUTPUT 2

<root>
  <row b="1" e="2" h="4" m="5" n="6" o="7" />
  <row b="1" e="2" h="4" m="8" n="9" o="0" />
  <row b="1" e="3" h="4" m="5" n="6" o="7" />
  <row b="1" e="3" h="4" m="8" n="9" o="0" />
<root>

Can anyone help my XSLT isn't very strong. Thanks.


It will be easier if you let the occurrence of <e> determine the outer loop constructing your <row>s and have a inner loop iterating over all <l>s.

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

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

    <xsl:template match="e">
        <!-- store values of 'e' and 'f' in params -->
        <xsl:param name="value_of_e" select="." />
        <xsl:param name="value_of_f" select="ancestor::d[1]/f" />
        <!-- iterate over all 'l's -->
        <xsl:for-each select="//l">
            <xsl:element name="row">
                <xsl:attribute name="b">
                    <xsl:value-of select="//b" />
                </xsl:attribute>
                <xsl:attribute name="e">
                    <xsl:value-of select="$value_of_e" />
                </xsl:attribute>
                <!-- only include 'f' if it has a value -->
                <xsl:if test="$value_of_f != ''">
                    <xsl:attribute name="f">
                        <xsl:value-of select="$value_of_f" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:attribute name="h">
                    <xsl:value-of select="ancestor::c[1]/g/@h" />
                </xsl:attribute>
                <xsl:attribute name="m">
                    <xsl:value-of select="./@m" />
                </xsl:attribute>
                <xsl:attribute name="n">
                    <xsl:value-of select="./@n" />
                </xsl:attribute>
                <xsl:attribute name="o">
                    <xsl:value-of select="./@o" />
                </xsl:attribute>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="b" />
    <xsl:template match="f" />
    <xsl:template match="g" />
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜