2015-02-07 103 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