2017-12-14 11 views
0

xmlファイルは2つあります。両方が(下のノードの多くを有する)xsltを使用してxml配列を別の種類のxml配列に変換します。

のアレイであるIは、XSLTを使用して

<queryCompoundEmployeeResponse> 
     <CompoundEmployee> 
      <id></id> 
      <person> 
       <lot of nodes/> 
      </person> 
     </CompoundEmployee> 
     <CompoundEmployee> 
      <id></id> 
      <person> 
       <lot of nodes/> 
      </person> 
     </CompoundEmployee> 
    </queryCompoundEmployeeResponse> 

にこれ

<result> 
     <sfobject> 
      <id></id> 
      <type>CompoundEmployee</type> 
      <person> 
       <lot of nodes/> 
      </person> 
     </sfobject> 
     <sfobject> 
      <id></id> 
      <type>CompoundEmployee</type> 
      <person> 
       <lot of nodes/> 
      </person> 
     </sfobject> 
    </result> 

を変換する必要があります。私はそれのためにこのxsltを持っています。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
     <xsl:output omit-xml-declaration="yes" indent="yes" /> 
     <xsl:strip-space elements="*" /> 
     <xsl:template match="node()|@*" name="identity"> 
      <xsl:copy> 
       <xsl:apply-templates select="node()|@*" /> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template match="sfobject/*[1]"> 
      <queryCompoundEmployeeResponse> 
       <CompoundEmployee> 
        <id> 
         <xsl:value-of select="@id" /> 
        </id> 
        <xsl:copy-of select="/*/person[1]" /> 
        <xsl:call-template name="identity" /> 
        </id> 
       </CompoundEmployee> 
      </queryCompoundEmployeeResponse> 
     </xsl:template> 
     <xsl:template match="/*/sfobject[1]" /> 
     <xsl:param name="removeElementsNamed" select="'type'"/> 
    </xsl:stylesheet> 

これはよくチェックアウトしません。私はこれまでgroovyでこれをやったことがあるが、今はシステムが変更されたときにこれをxsltに変換しなければならない。私はxsltには新しく、ここでは高度なレベルのxsltを使用すると確信しています。どのポインタも高く評価されています。

ここではxsltは適切なツールですか?または私はグルーヴィーに固執する必要がありますか?

答えて

1

あなたが実装する必要がある3つのルールを持って、それがsfobject下から

typeを削除type

  • で開催されているものにresult名前の変更sfobject
  • queryCompoundEmployeeResponseに名前を変更し

    1. です幸いにも、ここの各ルールは実際には実現可能です別のテンプレートとして分類されます。

      ので、ルール1のために、あなたはこれを行う...ルール2については

      <xsl:template match="result"> 
          <queryCompoundEmployeeResponse> 
           <xsl:apply-templates /> 
          </queryCompoundEmployeeResponse> 
      </xsl:template> 
      

      、これを行う...

      <xsl:template match="sfobject"> 
          <xsl:element name="{type}"> 
           <xsl:apply-templates select="node()|@*" /> 
          </xsl:element> 
      </xsl:template> 
      

      とルール3のため、これを行う...

      <xsl:template match="sfobject/type" /> 
      

      次に、IDテンプレートを使用して、他のすべてのノードと属性を処理します。

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
          <xsl:output omit-xml-declaration="yes" indent="yes" /> 
          <xsl:strip-space elements="*" /> 
      
          <xsl:template match="node()|@*" name="identity"> 
           <xsl:copy> 
            <xsl:apply-templates select="node()|@*" /> 
           </xsl:copy> 
          </xsl:template> 
      
          <xsl:template match="result"> 
           <queryCompoundEmployeeResponse> 
            <xsl:apply-templates /> 
           </queryCompoundEmployeeResponse> 
          </xsl:template> 
      
          <xsl:template match="sfobject"> 
           <xsl:element name="{type}"> 
            <xsl:apply-templates select="node()|@*" /> 
           </xsl:element> 
          </xsl:template> 
      
          <xsl:template match="sfobject/type" /> 
      </xsl:stylesheet> 
      
      このXSLTをお試しください
    関連する問題