2016-03-02 31 views
0

我是XSLT新手,正在尋找將重複節點/元素添加到現有列表的方法。這裏是一個例子xml。如何使用XSLT添加狗或貓的新實例?XSLT添加重複節點的新實例

`<?xml version="1.0" encoding="UTF-16"?> 
<Animals> 
    <Cats> 
     <Cat> 
      <Name>Felix</Name> 
      <Color>Orange</Color> 
      <Gender>Male</Gender> 
      <Age>5</Age> 
     </Cat> 
     <Cat> 
      <Name>Fluffy</Name> 
      <Color>White</Color> 
      <Gender>Female</Gender> 
      <Age>4</Age> 
     </Cat>  
     <Cat> 
      <Name>Shadow</Name> 
      <Color>Black</Color> 
      <Gender>Female</Gender> 
      <Age>2</Age> 
     </Cat>   
    </Cats> 
    <Dogs> 
     <Dog> 
      <Name>Spot</Name> 
      <Color>White/Brown/Black</Color> 
      <Gender>Male</Gender> 
      <Age>11</Age> 
     </Dog> 
     <Dog> 
      <Name>Rocky</Name> 
      <Color>Black</Color> 
      <Gender>Male</Gender> 
      <Age>8</Age> 
     </Dog>  
     <Dog> 
      <Name>Goldie</Name> 
      <Color>Gold</Color> 
      <Gender>Female</Gender> 
      <Age>3</Age> 
     </Dog>   
    </Dogs> 
</Animals>` 
+0

歡迎SO!你讀過的東西對你來說沒有意義,或者試圖失效? – J0e3gan

+0

新的狗或貓的屬性從哪裏來? –

+0

我會手動更新XSLT並使用它來用新數據更新XML。目前我的公司有一個類似的XML,我們更新它以將快捷方式添加到自定義外殼。某些位置需要額外的快捷方式,其他人不這樣做,所以目前我們有幾個版本的相同的XML。這是難以管理的,因爲下一次全球更新將抹去所述XML的「定製」版本。我們希望使用XSLT來管理這一點,但我們是一家小公司,我們都沒有經驗過XSLT。 – bcool78

回答

0

我將手動更新XSLT並使用它來更新帶有新數據的XML 。

我不認爲這是一個很好的方法,但是如果你願意,它可以很容易地做到爲:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="Cats"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <Cat> 
      <Name>New Cat</Name> 
      <Color>New Cat's Color</Color> 
      <Gender>New Cat's Gender</Gender> 
      <Age>New Cat's Age</Age> 
     </Cat> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Dogs"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <Dog> 
      <Name>New Dog</Name> 
      <Color>New Dog's Color</Color> 
      <Gender>New Dog's Gender</Gender> 
      <Age>New Dog's Age</Age> 
     </Dog> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

的明智之選將使用附加數據的外部XML文檔,例如:

Additions.xml

<Animals> 
    <Cats> 
     <Cat> 
      <Name>New Cat</Name> 
      <Color>New Cat's Color</Color> 
      <Gender>New Cat's Gender</Gender> 
      <Age>New Cat's Age</Age> 
     </Cat>  
    </Cats> 
    <Dogs> 
     <Dog> 
      <Name>New Dog</Name> 
      <Color>New Dog's Color</Color> 
      <Gender>New Dog's Gender</Gender> 
      <Age>New Dog's Age</Age> 
     </Dog> 
    </Dogs> 
</Animals> 

,並有與樣式表中的數據處理XML輸入結合起來:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- path to the additions document --> 
<xsl:param name="additions" select="'additions.xml'"/> 

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

<xsl:template match="Cats"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <xsl:apply-templates select="document($additions)/Animals/Cats/Cat"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Dogs"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <xsl:apply-templates select="document($additions)/Animals/Dogs/Dog"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

謝謝,完美的作品。你是男人!或女人,你可能是一個女人。大聲笑 – bcool78

0

如果你要每次手動更新XSLT ...您可以使用一些像這樣的事情,你需要不斷添加狗/貓節點要求

<?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" version="1.0" encoding="UTF-8" indent="yes"/> 

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

<xsl:template match="/Animals/Cats/Cat"> 
<Cat> 
     <Name>New</Name> 
     <Color>New</Color> 
     <Gender>New</Gender> 
     <Age>500</Age> 
    </Cat> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:copy> 
    </xsl:template> 
    <xsl:template match="/Animals/Dogs/Dog"> 
<Dog> 
     <Name>New</Name> 
     <Color>New</Color> 
     <Gender>New</Gender> 
     <Age>500</Age> 
    </Dog> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

這會爲每個現有的Cat添加一個新的Cat實例! –