0

我有以下具有父子关系的 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>
4

1 回答 1

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>
于 2013-04-30T09:19:01.227 回答