Я преобразую XML в XML, используя следующий XSLT.Мне нужно проверить исходный XML для требуемого элемента.Если значение узла-брата отсутствует для требуемого узла, то создайте новый узел.Вот XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Data Schema="XML A">
<xsl:apply-templates/>
</Data>
</xsl:template>
<xsl:template match="Attribute[not(Type=following::Type)]">
<Attributes type="{Type}">
<xsl:apply-templates
select="../Attribute[Type=current()/Type]" mode="out"/>
</Attributes>
</xsl:template>
<xsl:template match="Attribute" mode="out">
<Attr id="{id}" name="{Name}" value="{Value}"/>
</xsl:template>
<xsl:template match="Attribute"/>
</xsl:stylesheet>
Вот XML
<?xml version="1.0" encoding="windows-1252"?>
<XML>
<Attributes>
<Attribute>
<id>331</id>
<Name>Enviornment</Name>
<Type>common</Type>
<Value>Development</Value>
</Attribute>
<Attribute>
<id>79</id>
<Name>Retail</Name>
<Type>common</Type>
<Value></Value>
</Attribute>
<Attribute>
<id>402</id>
<Name>Gender</Name>
<Type>category</Type>
<Value>Men</Value>
</Attribute>
</Attributes>
</XML>
И если требуемые элементы отсутствуют, то он должен создать следующий XML.У меня есть несколько обязательных элементов.
<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
<Attributes type="common">
<Attr id="331" name="Enviornment" value="Development" />
<Attr id="79" name="Retail" value="" />
</Attributes>
<Attributes type="category">
<Attr id="402" name="Gender" value="Men" />
</Attributes>
<errorCodes>
<errorCode>"value for Retail is missing."</errorCode>
</errorCodes>
</Data>
И если это можно сделать с помощью следующего XSLT, это будет большим плюсом.Заранее спасибо.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="type" match="Attribute" use="Type"/>
<xsl:template match="/">
<Data Schema="XML A">
<xsl:apply-templates select="XML/Attributes/Attribute">
<xsl:sort select="Type" order="descending"/>
</xsl:apply-templates>
</Data>
</xsl:template>
<xsl:template
match="Attribute[generate-id()=generate-id(key('type', Type)[1])]">
<Attributes type="{Type}">
<xsl:apply-templates
select="../Attribute[Type=current()/Type]" mode="out"/>
</Attributes>
</xsl:template>
<xsl:template match="Attribute" mode="out">
<Attr id="{id}" name="{Name}" value="{Value}"/>
</xsl:template>
<xsl:template match="Attribute"/>
</xsl:stylesheet>