2011-03-22 2 views
1

다음 XSLT를 사용하여 XML을 XML로 변환 중입니다. 필요한 요소에 대해 소스 XML의 유효성을 검사해야합니다. 형제 노드의 값이 필수 노드에 대해 누락 된 경우 새 노드를 만듭니다. 다음은 여기에 XSLTXSLT XML to 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 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이다. 여러 필수 요소가 있습니다.

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

+0

질문이 1 개 더 있습니다. 특정 Attributes/Attribute/Type = ComplexAttr을 확인해야합니다. 소스에 있다면 안에 을 만들어야합니다. 그렇지 않으면 기본값으로 일부 값을 설정해야합니다. 이 산출물을 어떻게 달성 할 수 있는지 안내해 주시겠습니까? – JohnXsl

+0

위의 XSLT가 모두 체크하고있다. 그러나 필요한 특정 항목 만 유효성을 검사하려고합니다. 예를 들어 노드가/Attributes/Attribute/Name = Retail 인 경우/Attributes/Attribute/Value의 값을 확인한 다음 누락 된 경우 노드 – JohnXsl