2012-05-07 155 views
0

我想從父節點複製節點到子節點。我不確定如何實現這一目標。將父節點的一部分複製到子節點XSLT

我的源XML

<Root1> 
    <Family1> 
     <Childrens> 
     <Child> 
      <ChildFirstName>Robin1</ChildFirstName> 
      <ChildLastName>Jackman1</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 

       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     <Child> 
      <ChildFirstName>Robin2</ChildFirstName> 
      <ChildLastName>Jackman2</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 

       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     <Child> 
      <ChildFirstName>Robin3</ChildFirstName> 
      <ChildLastName>Jackman3</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 

       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     </Childrens> 
    </Family1> 
    </Root1> 

我想獲得輸出作爲

<Root1> 
    <Family1> 
     <Childrens> 
     <Child> 
      <ChildFirstName>Robin1</ChildFirstName> 
      <ChildLastName>Jackman1</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 
        <ChildFirstName>Robin1</ChildFirstName> 
        <ChildLastName>Jackman1</ChildLastName> 
        <Address> 
        <Street1>Street1</Street1> 
        <State>State</State> 
        <Country>Country</Country> 
        </Address> 
       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     <Child> 
      <ChildFirstName>Robin2</ChildFirstName> 
      <ChildLastName>Jackman2</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 
        <ChildFirstName>Robin2</ChildFirstName> 
        <ChildLastName>Jackman2</ChildLastName> 
        <Address> 
        <Street1>Street1</Street1> 
        <State>State</State> 
        <Country>Country</Country> 
        </Address> 
       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     <Child> 
      <ChildFirstName>Robin3</ChildFirstName> 
      <ChildLastName>Jackman3</ChildLastName> 
      <Address> 
      <Street1>Street1</Street1> 
      <State>State</State> 
      <Country>Country</Country> 
      </Address> 
      <Activities> 
      <Sports> 
       <Cricket> 
       <ParticipentNames> 
        <ChildFirstName>Robin3</ChildFirstName> 
        <ChildLastName>Jackman3</ChildLastName> 
        <Address> 
        <Street1>Street1</Street1> 
        <State>State</State> 
        <Country>Country</Country> 
        </Address> 
       </ParticipentNames> 
       </Cricket> 
      </Sports> 
      </Activities> 
     </Child> 
     </Childrens> 
    </Family1> 
    </Root1> 

我想在XSLT 1.0解決方案。

我想這些節點複製到子節點

    <ChildFirstName>Robin3</ChildFirstName> 
        <ChildLastName>Jackman3</ChildLastName> 
        <Address> 
        <Street1>Street1</Street1> 
        <State>State</State> 
        <Country>Country</Country> 
        </Address> 

感謝。

+0

反向XPath不推薦!!!! –

+0

您的源XML和期望的輸出非常大,您尚未準確解釋源XML文檔中的哪些節點應該完全複製到哪個元素下。此外,您的XSLT代碼引用了當前提供的源XML文檔中不存在的元素。由於這些事實,目前這個問題很混亂,很容易猜測。請編輯問題並精確指定所有必要的信息。 –

回答

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

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/OrderItem/ProductionInformation/GSItem"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:for-each select="/OrderItem"> 
     <xsl:apply-templates select="@*|node()[name()!='ProductionInformation']"/> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
相關問題