Extreme XSLT XML Уплощение - PullRequest
       11

Extreme XSLT XML Уплощение

0 голосов
/ 30 сентября 2011

У меня есть следующий входной 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, не очень сильный. Спасибо.

1 Ответ

0 голосов
/ 02 октября 2011

Будет проще, если вы позволите вхождению <e> определить внешний цикл, составляющий ваши <row> s, и будете иметь внутренний цикл, повторяющийся по всем <l> s.

Попробуйте что-то вроде этого:

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