2015-02-07 65 views
1

我有以下xslt。我注意到有時元素名稱「元組」有一個屬性。我想刪除該屬性並將其添加爲元素。我添加了測試來驗證'元組'是否有一個屬性,但它返回一個空白'ecatalogue'元素。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="atom"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="table"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
    <!--this test doesn't work properly --> 
    <xsl:template match="tuple"> 
     <xsl:choose> 
     <xsl:when test="@name"> 
      <xsl:apply-templates /> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- nothing to do 
      the node should stay the same 
     --> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    <!-- end test --> 
</xsl:stylesheet> 

結果我用上面的這個模板。

<ecatalogue> 

</ecatalogue> 

https://gist.github.com/guinslym/5ce47460a31fe4c4046b

+0

如果您提供了示例源XML文件以及您希望將其轉換爲什麼,這將會很有幫助。 – Mitch 2015-02-07 23:44:53

+0

感謝米奇!我的要點鏈接沒有工作... – 2015-02-07 23:48:36

+0

@PapoucheGuinslyzinho將屬性始終是'@名稱',不能有另一個名字? – 2015-02-08 03:24:54

回答

4

我注意到有時元素名稱'元組'有一個屬性。 I 想要移除該屬性並將其添加爲元素。我添加了一個測試 以驗證「元組」有一個屬性

這與XSLT工作時絕對不最好的辦法。您要變換的屬性,而不是它的父tuple元素 - 所以你的模板應與屬性直接,例如:

<xsl:template match="tuple/@*"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates /> 
    </xsl:element> 
</xsl:template> 

沒有測試此要求:如果該屬性存在,模板將匹配它並處理它;如果沒有,模板將不會被應用。

-

注:以上是假設您想要的屬性轉變成的tuple的子元素,同級其他的,已經存在的,children.Your交不上這一點很清楚。

2

通過以下調整到您的模板匹配tuple

<xsl:template match="tuple"> 
    <xsl:choose> 
    <xsl:when test="@name"> 
     <tuple> 
     <xsl:element name="{@name}"/> 
     <xsl:apply-templates /> 
     </tuple> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:copy> 
     <xsl:apply-templates /> 
     </xsl:copy> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

所有tuple節點沒有name屬性被複制:

<xsl:otherwise> 
    <xsl:copy> 
    <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:otherwise> 

,並在情況下,tuplename屬性,則tuple是沒有屬性寫的,屬性添加元素(沒有任何價值,因爲它目前還不清楚是否有任何值)和子節點複製:

<xsl:when test="@name"> 
    <tuple> 
    <xsl:element name="{@name}"/> 
    <xsl:apply-templates /> 
    </tuple> 
</xsl:when> 

的部分輸入XML作爲例子:

<tuple name="ObjManufacturerRef"> 
    <NamOrganisation>Unknown;:;Inconnu</NamOrganisation> 
    <NamOrganisationAcronym>Unknown</NamOrganisationAcronym> 
    <AddPhysCountry>Unknown</AddPhysCountry> 
</tuple> 

結果:

<tuple> 
    <ObjManufacturerRef/> 
    <NamOrganisation>Unknown;:;Inconnu</NamOrganisation> 
    <NamOrganisationAcronym>Unknown</NamOrganisationAcronym> 
    <AddPhysCountry>Unknown</AddPhysCountry> 
</tuple> 

保存例如:http://xsltransform.net/nc4NzQq/1具有添加<xsl:strip-space elements="*"/>刪除空格。

+0

謝謝@matthias_h的答案。我將對其進行修改,以便輸出:元組> ObjManufacturerRef> children [name或Name ...或AddPhy]。感謝xsltransform.net我不知道那個網站。 – 2015-02-08 00:18:10