2011-03-22 125 views
1

我正在使用以下XSLT將XML轉換爲XML。我需要驗證所需元素的源XML。如果所需節點的兄弟節點的值缺失,則創建一個新節點。 這裏是XSLTXSLT XML到XML轉換,驗證,動態創建節點/元素

<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> 

回答

6

以下樣式:

<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> 
+0

@Iwburk感謝您的幫助和快速響應。 – JohnXsl 2011-03-22 18:30:10

+0

我還有1個問題。我必須檢查一個特定的Attributes/Attribute/Type = ComplexAttr。如果它存在於源代碼中,那麼我必須在中創建,否則我必須將它默認爲某些值。你能指導我如何實現這個輸出。 – JohnXsl 2011-03-22 18:38:05

+0

上面的XSLT正在檢查所有這些事件。但是,我只想驗證所需的特定部分。例如,當節點爲/ Attributes/Attribute/Name = Retail時,檢查/ Attributes/Attribute/Value的值,如果缺失,則創建一個節點 – JohnXsl 2011-03-22 19:07:26