XSLT递归调用

时间:2013-04-30 06:35:56

标签: xml xslt

我有以下带有父子关系的XML

<EntityList>
    <item>
        <itemType>Parent</itemType>
        <itemInfo>
            <itemId>Parent1</itemId>
        </itemInfo>
        <childList>
            <item>
                <itemType>Case</itemType>
                <itemInfo>
                    <itemId>Case1</itemId>
                </itemInfo>
                <childList>
                    <itemGroup>
                        <itemType>Product</itemType>
                        <groupItem>
                            <groupItemInfo>
                                <itemId>Product1.0</itemId>
                            </groupItemInfo>
                            <groupItemInfo>
                                <itemId>Product1.1</itemId>
                            </groupItemInfo>
                        </groupItem>
                    </itemGroup>
                </childList>
            </item>
            <item>
                <itemType>Case</itemType>
                <itemInfo>
                    <itemId>Case2</itemId>
                </itemInfo>
                <childList>
                    <itemGroup>
                        <itemType>Product</itemType>
                        <groupItem>
                            <groupItemInfo>
                                <itemId>Product2.0</itemId>
                            </groupItemInfo>
                            <groupItemInfo>
                                <itemId>Product2.1</itemId>
                            </groupItemInfo>
                        </groupItem>
                    </itemGroup>
                </childList>
            </item> 
            <item>
                <itemType>Case</itemType>
                <itemInfo>
                    <itemId>Case3</itemId>
                </itemInfo>
                <childList>
                    <itemGroup>
                        <itemType>Product</itemType>
                        <groupItem>
                            <groupItemInfo>
                                <itemId>Product3.0</itemId>
                            </groupItemInfo>
                            <groupItemInfo>
                                <itemId>Product3.1</itemId>
                            </groupItemInfo>
                        </groupItem>
                        <!-- -->
                    </itemGroup>
                </childList>
            </item> 
        </childList>
    </item>
</EntityList>

我需要像这样输出

<?xml version="1.0" encoding="UTF-8"?>
<Body>
    <EntityList>
        <Event>
            <parentID>Parent1</parentID>
            <child>
                <name>Case1</name>
                                    <name>Case2</name>
                                    <name>Case3</name>
            </child>
        </Event>
        <Event>
            <parentID>Case1</parentID>
            <child>
                <name>product1.0</name>
                <name>product1.1</name>
            </child>
        </Event>
        <Event>
            <parentID>Case2</parentID>
            <child>
                <name>product2.0</name>
                <name>product2.1</name>
            </child>
        </Event>
        <Event>
            <parentID>Case3</parentID>
            <child>
                <name>product3.0</name>
                <name>product3.1</name>
            </child>
        </Event>
    </EntityList>
</Body>

通过使用这个xslt我能够获取parentID并获取相应parentID的子对我来说有点棘手。这意味着它必须以递归方式调用

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gs1ushc="http://epcis.gs1us.org/hc/ns" >
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
<xsl:template match="/">

    <Body>
        <EntityList>
        <xsl:for-each select="//itemAggr">
            <Event>
                <xsl:if test="//childList">
                <parentID><xsl:value-of select=".//itemId" /></parentID>
                <child>
                    ????
                </child>
            </xsl:if>
            </Event>
            </xsl:for-each>
        </EntityList>
    </Body>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

xmlns:gs1ushc="http://epcis.gs1us.org/hc/ns" >
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">

        <Body>
            <EntityList>
                <xsl:apply-templates select="EntityList/item"  mode="parent"/>
            </EntityList>
        </Body>
    </xsl:template>

    <xsl:template match ="item" mode="parent">
        <Event>
            <xsl:if test="childList">
                <parentID>
                    <xsl:value-of select="itemInfo/itemId" />
                </parentID>
                <child>
                    <xsl:apply-templates select="childList"  />
                </child>
            </xsl:if>
        </Event>
        <xsl:apply-templates select="childList/item" mode="parent"/>

    </xsl:template>
    <xsl:template match="itemType" />

    <xsl:template match ="item">
        <name>
            <xsl:value-of select="itemInfo/itemId" />
        </name>
    </xsl:template>
    <xsl:template match ="groupItemInfo">
        <name>
            <xsl:value-of select="itemId" />
        </name>
    </xsl:template>

</xsl:stylesheet>

生成以下输出:

<Body xmlns:gs1ushc="http://epcis.gs1us.org/hc/ns">
  <EntityList>
    <Event>
      <parentID>Parent1</parentID>
      <child>
        <name>Case1</name>
        <name>Case2</name>
        <name>Case3</name>
      </child>
    </Event>
    <Event>
      <parentID>Case1</parentID>
      <child>
        <name>Product1.0</name>
        <name>Product1.1</name>
      </child>
    </Event>
    <Event>
      <parentID>Case2</parentID>
      <child>
        <name>Product2.0</name>
        <name>Product2.1</name>
      </child>
    </Event>
    <Event>
      <parentID>Case3</parentID>
      <child>
        <name>Product3.0</name>
        <name>Product3.1</name>
      </child>
    </Event>
  </EntityList>
</Body>