XSLT XML到XML的转换、验证、动态创建节点/元素
我正在使用以下XSLT将XML转换为XML。我需要验证所需元素的源XML。如果所需节点缺少同级节点的值,则创建一个新节点。 这是XSLTXSLT XML到XML的转换、验证、动态创建节点/元素,xml,validation,xslt,required,Xml,Validation,Xslt,Required,我正在使用以下XSLT将XML转换为XML。我需要验证所需元素的源XML。如果所需节点缺少同级节点的值,则创建一个新节点。 这是XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <Data Schema="XML A"> <xsl:appl
<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>
331
环境
常见的
发展
79
零售
常见的
402
性别
类别
男人
如果缺少必需的元素,那么它应该创建以下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>
以下样式表:
<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>
<errorCodes>
<xsl:apply-templates select="XML/Attributes/Attribute"
mode="errors"/>
</errorCodes>
</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:template match="Attribute" mode="errors"/>
<xsl:template match="Attribute[Value='']" mode="errors">
<errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
</xsl:template>
</xsl:stylesheet>
“缺少的值。”
产生所需的输出:
<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>
“缺少零售价值。”
以下样式表:
<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>
<errorCodes>
<xsl:apply-templates select="XML/Attributes/Attribute"
mode="errors"/>
</errorCodes>
</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:template match="Attribute" mode="errors"/>
<xsl:template match="Attribute[Value='']" mode="errors">
<errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
</xsl:template>
</xsl:stylesheet>
“缺少的值。”
产生所需的输出:
<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>
“缺少零售价值。”
我还有一个问题。我必须检查特定的Attributes/Attribute/Type=ComplexAttr。如果它存在于源代码中,那么我必须在内部创建,否则我必须将其默认为某些值。您能指导我如何实现这一输出吗?以上XSLT的另一件事是检查所有这些输出。但是,我只想验证所需的特定选项。例如,当节点为/Attributes/Attribute/Name=Retail时,检查/Attributes/Attribute/value的值,如果缺少该值,则创建一个node@JohnXsl-你应该为Iwburk的出色工作投票。是的,我还有一个问题。我必须检查特定的Attributes/Attribute/Type=ComplexAttr。如果它存在于源代码中,那么我必须在内部创建,否则我必须将其默认为某些值。您能指导我如何实现这一输出吗?以上XSLT的另一件事是检查所有这些输出。但是,我只想验证所需的特定选项。例如,当节点为/Attributes/Attribute/Name=Retail时,检查/Attributes/Attribute/value的值,如果缺少该值,则创建一个node@JohnXsl-你应该为Iwburk的出色工作投票。是的。