2016-03-21 50 views
0

を使用してNULL値を排除し、次の出力フォーマットには、ルート要素の名前を変更し、私は、XML入力次変換しなければならなかったXSLT

<Standards xmlns="http://ws.wso2.org/dataservice"> 
     <Standard> 
      <ProductID>200057</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1001</SNumber> 
      <DraftProducts> 
       <RelatedProduct> 
        <ProductID>1500163</ProductID> 
       </RelatedProduct> 
      </DraftProducts> 
      <ReferenceProducts> 
       <RelatedProduct> 
        <ProductID>263973</ProductID> 
        <RelationId>708519</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320056</ProductID> 
        <RelationId>934789</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </ReferenceProducts> 
      <RelatedIntProducts> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116881</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208076</ProductID> 
        <RelationId>116886</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedIntProducts> 
      <S1>1001</S1> 
      <S2>1986</S2> 
     </Standard> 
     <Standard> 
      <ProductID>200058</ProductID> 
      <Prefix>ISO</Prefix> 
      <SNumber>1002</SNumber> 
      <DraftProducts> 
       <RelatedProduct> 
        <ProductID>1500167</ProductID> 
       </RelatedProduct> 
      </DraftProducts> 
      <ReferenceProducts> 
       <RelatedProduct> 
        <ProductID>263974</ProductID> 
        <RelationId>708519</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>320052</ProductID> 
        <RelationId>934754</RelationId> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </ReferenceProducts> 
      <RelatedIntProducts> 
       <RelatedProduct> 
        <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
        <RelationId>116837</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation>NEN ISO 1001</Designation> 
       </RelatedProduct> 
       <RelatedProduct> 
        <ProductID>208074</ProductID> 
        <RelationId>116843</RelationId> 
        <Relationship>Identical</Relationship> 
        <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       </RelatedProduct> 
      </RelatedIntProducts> 
      <S1>1005</S1> 
      <S2>1983</S2> 
     </Standard> 
    </Standards> 

<Standards xmlns="http://ws.wso2.org/dataservice"> 
    <Product> 
     <ProductID>200057</ProductID> 
     <Prefix>ISO</Prefix> 
     <SNumber>1001</SNumber> 
     <RelatedProducts> 
      <RelatedProduct> 
       <ProductID>1500163</ProductID> 
       <RelationType>DraftProducts</RelationType> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>263973</ProductID> 
       <RelationId>708519</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>320056</ProductID> 
       <RelationId>934789</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <RelationId>116881</RelationId> 
       <Relationship>Identical</Relationship> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Designation>NEN ISO 1001</Designation> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>208076</ProductID> 
       <RelationId>116886</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
     </RelatedProducts> 
     <S1>1001</S1> 
     <S2>1986</S2> 
    </Product> 
    <Product> 
     <ProductID>200058</ProductID> 
     <Prefix>ISO</Prefix> 
     <SNumber>1002</SNumber> 
     <RelatedProducts> 
      <RelatedProduct> 
       <ProductID>1500167</ProductID> 
       <RelationType>DraftProducts</RelationType> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>263974</ProductID> 
       <RelationId>708519</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>320052</ProductID> 
       <RelationId>934754</RelationId> 
       <RelationType>ReferenceProducts</RelationType> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <RelationId>116837</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation>NEN ISO 1001</Designation> 
      </RelatedProduct> 
      <RelatedProduct> 
       <ProductID>208074</ProductID> 
       <RelationId>116843</RelationId> 
       <RelationType>RelatedIntProducts</RelationType> 
       <Relationship>Identical</Relationship> 
       <Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </RelatedProduct> 
     </RelatedProducts> 
     <S1>1005</S1> 
     <S2>1983</S2> 
    </Product> 
</Standards> 

他の人の助けを借りて、私はこの変換を行うために次のxsltを書くことができました。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://ws.wso2.org/dataservice" xmlns="http://ws.wso2.org/dataservice" 
    exclude-result-prefixes="x" version="1.0"> 

    <xsl:output indent="yes" method="xml" /> 

    <!-- Identity Transform --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="x:Standard"> 
     <Product> 
      <xsl:copy-of select="x:ProductID" /> 
      <xsl:copy-of select="x:Prefix" /> 
      <xsl:copy-of select="x:SNumber" /> 
      <xsl:copy-of select="x:Year" /> 
      <xsl:copy-of select="x:Designation" /> 
      <xsl:copy-of select="x:Publisher" /> 
      <RelatedProducts> 
       <xsl:apply-templates select=".//x:RelatedProduct" /> 
      </RelatedProducts> 
      <xsl:copy-of select="x:ProductICSCodes" /> 
      <xsl:copy-of select="x:ProductDescriptions" /> 
      <xsl:copy-of select="x:IndustryCodes" /> 
      <xsl:copy-of select="x:ProductAttributes" /> 
      <xsl:copy-of select="x:ProductReconfirmationNotices" /> 
      <xsl:copy-of select="x:Type" /> 
      <xsl:copy-of select="x:SaleItems" /> 
      <xsl:copy-of select="x:Committee" /> 
      <xsl:copy-of select="x:CommentClosesDate" /> 
      <xsl:copy-of select="x:Note" /> 
      <xsl:copy-of select="x:InternalNote" /> 
      <xsl:copy-of select="x:DummyProductLink" /> 
      <xsl:copy-of select="x:Status" /> 
      <xsl:copy-of select="x:State" /> 
      <xsl:copy-of select="x:UpdateNote" /> 
      <xsl:copy-of select="x:ThumbnailPath" /> 
      <xsl:copy-of select="x:SortOrder" /> 
      <xsl:copy-of select="x:EntryDate" /> 
      <xsl:copy-of select="x:ExpiryDate" /> 
      <xsl:copy-of select="x:SupersededDate" /> 
      <xsl:copy-of select="x:UpdateDate" /> 
      <xsl:copy-of select="x:WithdrawnDate" /> 
      <xsl:copy-of select="x:PluginName" /> 
      <xsl:copy-of select="x:S1" /> 
      <xsl:copy-of select="x:S2" /> 
      <xsl:copy-of select="x:S3" /> 
      <xsl:copy-of select="x:S4" /> 
      <xsl:copy-of select="x:S5" /> 
      <xsl:copy-of select="x:S6" /> 
      <xsl:copy-of select="x:S7" /> 
      <xsl:copy-of select="x:S8" /> 
      <xsl:copy-of select="x:S9" /> 
      <xsl:copy-of select="x:S10" /> 
      <xsl:copy-of select="x:HasLexConnect" /> 
      <xsl:copy-of select="x:HasCodeConnect" /> 
     </Product> 
    </xsl:template> 
    <xsl:template match="x:RelatedProduct"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|*[not(name()='RelationId')]" /> 
      <RelationType> 
       <xsl:value-of select="name(..)" /> 
      </RelationType> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

今私は製品代わりの基準としてルート要素の名前を変更し、出力からすべてゼロ値を排除する必要があります。 nilの値は次の形式です。

<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
     <Abstract xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
     <History xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
     <TOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 

Iは製品としてルート要素の名前を変更し、次のXSLTセグメントを追加しました。

<xsl:template match="x:Standards"> 
    <Products namespace="{namespace-uri()}"> 
     <xsl:apply-templates select=".//x:Standard" /> 
    </Products> 
</xsl:template> 

これは間違いありませんか?上記のxsltを変更して、nilの値を出力から削除するにはどうすればよいですか。どんな助けもありがとう。

+0

を_は_ "正しいということです" - あなたが期待していた成果を生み出しましたか?ここで質問をすることで、すべての変更をチェックアウトする必要はありません。それを試してみてください! 'xsi:nil =" true "'で値を取り除くことに関しては、それに合ったテンプレートを書く方法を知っています。ちょうどテンプレートを空にするだけです。 –

+0

私は期待される出力を得ました。しかし、私はその価値をまだ取り除くことができませんでした。 –

答えて

1

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:nil

<xsl:template match="@xsi:nil"/> 

を無視して、あなたのxsl:copy-ofxsl:apply-templatesにを変更するためのテンプレートを追加するには、スタイルシートにXSI用namesace宣言を追加します。

<xsl:copy-of select="x:ProductID" /> 

へ:

<xsl:apply-templates select="x:ProductID" /> 

更新:
はnil属性トライを持つすべてのノードを無視するには、次の例の変更については -

<xsl:template match="*[@xsi:nil]"/> 
+0

私は提案されたアプローチを試みましたが、今は空のxmlタグを次のようにしています。 <指定/>は無要素です。要件は、すべてのnilノードを削除することです。 –

+0

はい - これはあなたが期待している帽子だと思いました。しかし、あなたが無制限の要素を無視したいと思うように聞こえます。 –

+0

あなたの親切な法人のためにありがとうございました。私は少しタイトなスケジュールで、実験するのに十分な時間がありません。再度、感謝します。 :) –

関連する問題