2012-05-09 6 views
2

XSLTを使用して変換する必要があるXMLがあります。 XMLに解決されていないカスタムタグがあり、結果が表示されません。続きXSLT変換でカスタムタグが機能しない

はXMLコードです:

<?xml version="1.0" encoding="UTF-16"?> 
<di:itg_dataImport xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd"> 
    <request> 
    <requestType>Project Issue</requestType> 
    <identifier>3</identifier> 
    <common:simpleField> 
     <common:token>REQ.ASSIGNED_TO_USER_ID</common:token> 
     <common:stringValue>Admin User (DEV)</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.DESCRIPTION</common:token> 
     <common:stringValue>Test - Pls Ignore</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_ESCALATION_LEVEL</common:token> 
     <common:stringValue>Project</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_MASTER_PROJ_REF</common:token> 
     <common:stringValue>P0912002 IPTV Residential Phase 1</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.PRIORITY_CODE</common:token> 
     <common:stringValue>Normal</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.WORKFLOW_ID</common:token> 
     <common:stringValue>Issue Management Process</common:stringValue> 
    </common:simpleField> 
    </request> 

</di:itg_dataImport> 

XSLTが呼び出されている次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
<requests> 
    <request> 
    <requestType> 
    <xsl:copy-of select="di:itg_dataImport/request/requestType"/> 
    </requestType> 

    </request> 
</requests> 

</xsl:template> 
</xsl:stylesheet> 

所望の出力は次のようになります。

プロジェクト課題

は誰が助けてください私が間違っているところを解決して指しています。 おかげ

答えて

1

XSLファイル:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
    xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
    xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
     <requests> 
      <request> 
       <xsl:for-each select="descendant::*[local-name() = 'requestType']"> 
        <requestType> 
         <xsl:value-of select="text()"/> 
        </requestType> 
       </xsl:for-each> 
      </request> 
     </requests> 
    </xsl:template> 
</xsl:stylesheet> 

結果、文書

<?xml version="1.0" encoding="utf-8"?> 
<requests xmlns="http://www.mercury.com/itg/dm/2.0/types" xmlns:di="http://www.mercury.com/itg/data_import/1.0" xmlns:common="http://www.mercury.com/itg/common/2.0/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <request> 
     <requestType>Project Issue</requestType> 
    </request> 
</requests> 
+1

これはうまくいきますが、個人的には、コードに間違っていることを人に伝えることをお勧めします。そうすれば、少し違った問題で同じミスをやり直すと、より多くの助けを得ることができます。 –

1

それはナンバーワンXSLTのコーディングエラーです:あなたの要素は(デフォルト)名前空間にあり、それらを選択するときに名前空間を指定していません。ここでは、マイケル・ケイは正しく、より明示的に指し示すものを作るために

+0

+1、私はこの分野では非常に初心者であり、あなたの本を読んで知識を集めています。大変ありがとうございました。根本的な原因を示してくれてありがとうございます。 – Cylian

1

は、デフォルトの名前空間内のドキュメントで作業する方法です - あなたの特定のケースに:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns:x = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
<requests> 
    <request> 
    <requestType> 
    <xsl:copy-of select="di:itg_dataImport/x:request/x:requestType"/> 
    </requestType> 

    </request> 
</requests> 
</xsl:template> 
</xsl:stylesheet> 

私はわずか数作りましたあなたのオリジナルのコードへの変更:

  1. は、XSLTスタイルシートは、もはやデフォルトの名前空間を持っていない - その代わりに、接頭辞で指定された同じ名前空間を持っています。

  2. select属性のxsl:copy-ofには、すべての名前の接頭辞が付いています。所望の、正しい結果が生成さ

    <di:itg_dataImport xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
            xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
            xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
            xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd"> 
        <request> 
        <requestType>Project Issue</requestType> 
        <identifier>3</identifier> 
        <common:simpleField> 
         <common:token>REQ.ASSIGNED_TO_USER_ID</common:token> 
         <common:stringValue>Admin User (DEV)</common:stringValue> 
        </common:simpleField> 
        <common:simpleField> 
         <common:token>REQ.DESCRIPTION</common:token> 
         <common:stringValue>Test - Pls Ignore</common:stringValue> 
        </common:simpleField> 
        <common:simpleField> 
         <common:token>REQ.KNTA_ESCALATION_LEVEL</common:token> 
         <common:stringValue>Project</common:stringValue> 
        </common:simpleField> 
        <common:simpleField> 
         <common:token>REQ.KNTA_MASTER_PROJ_REF</common:token> 
         <common:stringValue>P0912002 IPTV Residential Phase 1</common:stringValue> 
        </common:simpleField> 
        <common:simpleField> 
         <common:token>REQ.PRIORITY_CODE</common:token> 
         <common:stringValue>Normal</common:stringValue> 
        </common:simpleField> 
        <common:simpleField> 
         <common:token>REQ.WORKFLOW_ID</common:token> 
         <common:stringValue>Issue Management Process</common:stringValue> 
        </common:simpleField> 
        </request> 
    
    </di:itg_dataImport> 
    

    <requests xmlns:di="http://www.mercury.com/itg/data_import/1.0" xmlns:x="http://www.mercury.com/itg/dm/2.0/types" xmlns:common="http://www.mercury.com/itg/common/2.0/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
        <request> 
         <requestType> 
          <requestType xmlns="http://www.mercury.com/itg/dm/2.0/types">Project Issue</requestType></requestType> 
        </request> 
    </requests> 
    

    ルールがを覚えてこの変換を提供XML文書に適用され

:XPathは、接頭辞のない名前を「名前なし」に属するものとして扱います。スペース"。これに対処するには、XSLTコードでこの名前空間を定義します。接頭辞を付けて、名前の前にこの接頭辞を付けます。

+0

+1、unputdownable! – Cylian

+0

@Cylian:どうぞよろしくお願いいたします。 –

関連する問題