XSLT将XML转换为层次结构格式

时间:2015-08-30 12:10:58

标签: xml xslt

我有一个简单形式的带有partyId和parentPartyId值的xml。我想将其转换为分层树格式的形式。

我已根据所需输出创建了架构。我正在http://www.keller.com/xslt/8/中尝试Axis Names表达式。我没有得到,如何将源转换为所需的格式?

来源和要求的目标如下。

来源:

<OutputCollection xmlns="http://xmlns.test.com/pcbpel/adapter/db/PartyHierarchy">
            <Output>
               <level>1</level>
               <Parent_Party xsi:nil="true"/>
               <Party>Party-1</Party>
            </Output>
            <Output>
               <level>2</level>
               <Parent_Party>Party-1</Parent_Party>
               <Party>Party-1-1</Party>
            </Output>
            <Output>
               <level>2</level>
               <Parent_Party>Party-1</Parent_Party>
               <Party>Party-1-2</Party>
            </Output>
            <Output>
               <level>3</level>
               <Parent_Party>Party-1-2</Parent_Party>
               <Party>Party-1-2-1</Party>
            </Output>
            <Output>
               <level>3</level>
               <Parent_Party>Party-1-2</Parent_Party>
               <Party>Party-1-2-2</Party>
            </Output>
            <Output>
               <level>3</level>
               <Parent_Party>Party-1-2</Parent_Party>
               <Party>Party-1-2-3</Party>
            </Output>
            <Output>
               <level>2</level>
               <Parent_Party>Party-1</Parent_Party>
               <Party>Party-1-3</Party>
            </Output>
            <Output>
               <level>3</level>
               <Parent_Party>Party-1-3</Parent_Party>
               <Party>Party-1-3-1</Party>
            </Output>
            <Output>
               <level>3</level>
               <Parent_Party>Party-1-3</Parent_Party>
               <Party>Party-1-3-2</Party>
            </Output>
         </OutputCollection>

:定位:

<OutputCollection xmlns="http://xmlns.test.com/pcbpel/adapter/db/PartyHierarchy">
            <Output>
               <level>1</level>
               <Parent_Party xsi:nil="true"/>
               <Party>Party-1</Party>
               <Children>
                    <level>2</level>
                    <Parent_Party>Party-1</Parent_Party>
                    <Party>Party-1-1</Party>
               </Children>
               <Children>
                    <level>2</level>
                    <Parent_Party>Party-1</Parent_Party>
                    <Party>Party-1-2</Party>
                    <Children>
                        <level>3</level>
                        <Parent_Party>Party-1-2</Parent_Party>
                        <Party>Party-1-2-1</Party>
                    </Children>
                    <Children>
                        <level>3</level>
                        <Parent_Party>Party-1-2</Parent_Party>
                        <Party>Party-1-2-2</Party>
                    </Children>
                    <Children>
                        <level>3</level>
                        <Parent_Party>Party-1-2</Parent_Party>
                        <Party>Party-1-2-3</Party>                  
                    </Children>
               </Children>
               <Children>
                    <level>2</level>
                    <Parent_Party>Party-1</Parent_Party>
                    <Party>Party-1-3</Party>
                    <Children>
                        <level>3</level>
                        <Parent_Party>Party-1-3</Parent_Party>
                        <Party>Party-1-3-1</Party>                  
                    </Children>
                    <Children>
                        <level>3</level>
                        <Parent_Party>Party-1-3</Parent_Party>
                        <Party>Party-1-3-2</Party>          
                    </Children>                 
               </Children>
            </Output>
         </OutputCollection>

1 个答案:

答案 0 :(得分:2)

使用XSLT,如果您需要遵循交叉引用,那么您需要定义一个键<xsl:key name="children" match="ph:Output" use="ph:Parent_Party"/>并使用它<xsl:apply-templates select="key('children', ph:Party)"/>

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ph="http://xmlns.test.com/pcbpel/adapter/db/PartyHierarchy"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.test.com/pcbpel/adapter/db/PartyHierarchy"
  exclude-result-prefixes="ph xsi"
  version="1.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="children" match="ph:Output" use="ph:Parent_Party"/>

<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="key('children', '')"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="ph:Output[ph:Parent_Party/@xsi:nil = 'true']">
  <xsl:copy>
    <xsl:copy-of select="node()"/>
    <xsl:apply-templates select="key('children', ph:Party)"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="ph:Output">
  <children>
    <xsl:copy-of select="node()"/>
    <xsl:apply-templates select="key('children', ph:Party)"/>
  </children>
</xsl:template>

</xsl:stylesheet>

请注意,您发布的输入示例使用前缀xsi但未声明它,因此我不得不假设该前缀的命名空间。

相关问题