0

我有以下输入 XML 文件:

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

我想使用 XSLT 将其转换为以下输出:

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

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

谁能帮助我的 XSLT 不是很强大。谢谢。

4

1 回答 1

0

<e>如果您让 s 的出现确定构建您的 s 的外部循环<row>并有一个内部循环遍历所有s ,这将更容易<l>

尝试这样的事情:

<?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>
于 2011-10-02T13:02:01.520 回答