2017-07-25 48 views
0

我有一个扁平的xml文档。节点确实包含指示其是父节点还是子节点的信息,并且按照子节点跟随其父节点的顺序排列。我需要一个XSLT文件来将输入xml转换成一个xml文件,其中的子节点有一个链接返回到它们的父节点。将子/父链接添加到XSLT中的平面XML文件中

输入XML文件:

<items> 
    <item type="a"> 
     <value ID="11111" relation="parent"></value> 
    </item> 
    <item type="b"> 
     <value ID="22222" relation="child"></value> 
    </item> 
    <item type="b"> 
     <value ID="33333" relation="child"></value> 
    </item> 
    <item type="a"> 
     <value ID="44444" relation="parent"></value> 
    </item> 
    <item type="b"> 
     <value ID="55555" relation="child"></value> 
    </item> 
    <item type="a"> 
     <value ID="66666" relation="parent"></value> 
    </item> 
    <item type="a"> 
     <value ID="77777" relation="parent"></value> 
    </item> 
    <item type="b"> 
     <value ID="88888" relation="child"></value> 
    </item> 
    <item type="b"> 
     <value ID="99999" relation="child"></value> 
    </item> 
    <item type="b"> 
     <value ID="00000" relation="child"></value> 
    </item> 
</items> 

我想把它转换成:

<items> 
<item> 
    <itemType>a</itemType> 
    <itemID>11111</itemID> 
    <itemRelationship>parent</itemRelationship> 
    <itemParentID /> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>22222</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>33333</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
<item> 
    <itemType>a</itemType> 
    <itemID>44444</itemID> 
    <itemRelationship>parent</itemRelationship> 
    <itemParentID /> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>55555</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
<item> 
    <itemType>a</itemType> 
    <itemID>66666</itemID> 
    <itemRelationship>parent</itemRelationship> 
    <itemParentID /> 
</item> 
<item> 
    <itemType>a</itemType> 
    <itemID>77777</itemID> 
    <itemRelationship>parent</itemRelationship> 
    <itemParentID /> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>88888</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>99999</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
<item> 
    <itemType>b</itemType> 
    <itemID>00000</itemID> 
    <itemRelationship>child</itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</item> 
</items> 

凡“插入父这里ID”是由孩子的父母节点ID代替。

这里是我当前的XSLT文件:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
    <items> 
     <xsl:for-each select="items/item"> 
      <item> 
       <xsl:variable name = "vCurrentItemType" select ="@type"/> 
       <xsl:choose> 
        <xsl:when test = "$vCurrentItemType = 'a'"> 
         <xsl:call-template name="DrawParent"> 
          <xsl:with-param name = "pCurrentItem" select = "current()"/> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:call-template name="DrawChild"> 
          <xsl:with-param name = "pCurrentItem" select = "current()"/> 
         </xsl:call-template> 
        </xsl:otherwise> 
       </xsl:choose> 
      </item> 
     </xsl:for-each> 
    </items> 
</xsl:template> 

<xsl:template name = "DrawParent"> 
    <xsl:param name = "pCurrentItem" /> 
    <itemType> 
     <xsl:value-of select ="$pCurrentItem/@type"/> 
    </itemType> 
    <itemID> 
     <xsl:value-of select ="$pCurrentItem/value/@ID"/> 
    </itemID> 
    <itemRelationship> 
     <xsl:value-of select ="$pCurrentItem/value/@relation"/> 
    </itemRelationship> 
    <itemParentID></itemParentID> 
</xsl:template> 

<xsl:template name = "DrawChild"> 
    <xsl:param name = "pCurrentItem" /> 
    <itemType> 
     <xsl:value-of select ="$pCurrentItem/@type"/> 
    </itemType> 
    <itemID> 
     <xsl:value-of select ="$pCurrentItem/value/@ID"/> 
    </itemID> 
    <itemRelationship> 
     <xsl:value-of select ="$pCurrentItem/value/@relation"/> 
    </itemRelationship> 
    <itemParentID>insert parent ID here</itemParentID> 
</xsl:template> 
</xsl:stylesheet> 

回答

1

你想为你的父母ID表达是这样的....

<xsl:value-of select="$pCurrentItem/preceding-sibling::item[@type='a'][1]/value/@ID" /> 

但是,你可能考虑简化您的XSLT这一点,以避免一些代码重复...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="item"> 
     <item> 
      <itemType> 
       <xsl:value-of select="@type"/> 
      </itemType> 
      <itemID> 
       <xsl:value-of select="value/@ID"/> 
      </itemID> 
      <itemRelationship> 
       <xsl:value-of select="value/@relation"/> 
      </itemRelationship> 
      <itemParentID> 
       <xsl:if test="@type='b'"> 
        <xsl:value-of select="preceding-sibling::item[@type='a'][1]/value/@ID" />     
       </xsl:if> 
      </itemParentID> 
     </item> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet>