2017-05-04 7 views
0

この質問は実際に別のmy questionに関連しています。私は、サンプルxml、xsltスタイルシート、および変換されたXML出力ファイルを以下に示します。まず、出力ファイルに値を持たない要素を生成させたくないということです。 2つ目は、結果ファイルの最初のタグからこれらの名前空間宣言を削除したいということです。XSLT変換されたxmlの空の要素を削除します

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext"> 
    <soap:Body> 
     <InstantIDResponseEx xmlns="http://webservices.seisint.com/WsIdentity"> 
      <response> 
       <Header> 
        <Status>0</Status> 
        <TransId>41904</TransId> 
        <User></User> 
       </Header> 
      <Result> 
       <Data/> 
      </Result> 
     </response> 
    </InstantIDResponseEx> 
</soap:Body> 

XSLTスタイルシート:

 <?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:Identity="http://webservices.seisint.com/WsIdentity" 
       xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext" version="1.0"> 
<xsl:strip-space elements="*"/> 


    <!-- ****** MAIN TEMPLATE ******* --> 
    <xsl:template match="/"> 
     <Verification_response> 
       <xsl:apply-templates select="/soap:Envelope/soap:Body/Identity:InstantIDResponseEx/Identity:response"/> 
     </Verification_response> 
    </xsl:template> 

    <xsl:template match = "Identity:response"> 
     <System_Data> 
     <xsl:apply-templates select = "Identity:Header" /> 
     </System_Data> 
     <Received> 
     <xsl:apply-templates select = "Identity:Result" /> 
     </Received> 
    </xsl:template> 

    <xsl:template match="Identity:Header"> 
     <STATUSMESSAGE> 
     <xsl:apply-templates select="Identity:Status"/> 
     </STATUSMESSAGE> 
     <TRANSACTIONID> 
     <xsl:apply-templates select="Identity:TransId"/>  
     </TRANSACTIONID> 
     <Client> 
     <xsl:apply-templates select="Identity:User"/> 
     </Client> 
    </xsl:template> 

    <xsl:template match="Identity:Result"> 
     <Content> 
     <xsl:apply-templates select="Identity:Data"/> 
     </Content> 
    </xsl:template> 

    <xsl:template match="Identity:Status"> 
     <xsl:value-of select="."/> 
    </xsl:template> 

    <xsl:template match="Identity:TransId"> 
     <xsl:value-of select="."/> 
    </xsl:template>  

    <xsl:template match="Identity:User"> 
     <xsl:value-of select="."/> 
    </xsl:template> 

    <xsl:template match="Identity:Data"> 
     <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 

変換されたXML:

<Verification_response xmlns:Identity="http://webservices.seisint.com/WsIdentity" 
         xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
         xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext"> 
    <System_Data> 
     <STATUSMESSAGE>0</STATUSMESSAGE> 
     <TRANSACTIONID>41904</TRANSACTIONID> 
     <Client/> 
    </System_Data> 
    <Received> 
     <Content/> 
    </Received> 
</Verification_response> 
結果のXMLファイルは、この

<Verification_response> 
    <System_Data> 
     <STATUSMESSAGE>0</STATUSMESSAGE> 
     <TRANSACTIONID>41904</TRANSACTIONID> 
    </System_Data> 
</Verification_response> 

XML入力ファイルのようになります。

フォーラムの別の投稿からルートノードの前に次のコードを追加しようとしましたが、機能しませんでした。誰でもこの問題について助けてくれますか?

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

<xsl:template match= 
    "*[not(node())] 
    | 
    *[not(node()[2]) 
    and 
    node()/self::text() 
    and 
    not(normalize-space()) 
    ] 
    "/> 

答えて

0

あなたxsl:stylesheet要素に次の属性を追加し、名前空間宣言を削除するには:exclude-result-prefixes="Identity wsse soap"

空の要素を削除するには、スタイルシートの構造を変更する必要があります。ここでの問題は、コンテンツが存在するかどうかをチェックせずにリテラルの結果要素ラッパーを作成していることです。だから、条件付きで、それらを作成するために、いずれかの必要がある - 、例えば変更:に

<Received> 
    <xsl:apply-templates select = "Identity:Result" /> 
</Received> 

を:

<xsl:if test="Identity:Result//text()"> 
    <Received> 
     <xsl:apply-templates select = "Identity:Result" /> 
    </Received> 
</xsl:if> 

Identity:Resultテンプレートマッチングにラッパーの作成を移動して、条件付きで、それにテンプレートを適用します。これはない子孫TEを持たないように "空" 解釈する

<xsl:apply-templates select = "Identity:Result[.//text()]" /> 

注xtノード。

関連する問題