2016-07-06 134 views
0

我想聚合一个节点的所有子节点中存在的布尔值并将其添加到其父节点。 我的XML文档的样子:xslt儿童属性的布尔聚合

<?xml version="1.0" encoding="UTF-8"?> 
<module> 
    <entity name="test" > 
    <attributes name="att1" translatable="true"> 
    <attributes name="att2" translatable="false"> 
    <attributes name="att3" translatable="false"> 
    <attributes name="att4" translatable="true"> 
    </attributes> 
    </entity> 
</module> 

并将其转换为:

<?xml version="1.0" encoding="UTF-8"?> 
<module> 

    <!-- 
     At the entity level the property translatable shall be the result of 
     the OR aggregation of all children attributes.translatable 
     i.e. 
     iterate for all attributes (true OR false OR false OR true = true) ==> entity.translatable=true 

    --> 

<entity name="test" translatable="true"> 
    <attributes name="att1" translatable="true"> 
    <attributes name="att2" translatable="false"> 
    <attributes name="att3" translatable="false"> 
    <attributes name="att4" translatable="true"> 
    </attributes> 
    </entity> 
</module> 
+0

,使其结构良好的请编辑源XML。 –

+0

你在新的xml中添加translatable =“true”吗? – Developer

回答

1

迭代所有属性(真或假或假或真=真) ==> entity.translatable = true

无需迭代 - 只需要询问是否至少有一个属性值为真实

<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="entity"> 
    <entity name="{@name}" translatable="{boolean(attribute[@translatable='true'])}" > 
     <xsl:apply-templates/> 
    </entity> 
</xsl:template> 

</xsl:stylesheet> 

上述假定合式 XML输入,如:

<module> 
    <entity name="test"> 
    <attribute name="att1" translatable="true"/> 
    <attribute name="att2" translatable="false"/> 
    <attribute name="att3" translatable="false"/> 
    <attribute name="att4" translatable="true"/> 
    </entity> 
</module>