2016-12-30 14 views
0

xsltを使用してxsltを使用してロードブロッキングを実行し、SSRS 2008v2レンダリングXMLファイルを第三者EDI転送の別のXSL RAW形式に変換しました。私はこのサイトや他の人たちをしばらく検索してきましたが、それをまとめて苦労しました。私は次の生のxmlデータから始めます。xsltを使用したxmlファイルへの複数の変換1

<Invoices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.spscommerce.com/RSX" xsi:schemaLocation="http://www.spscommerce.com/RSX http://tfl- sql01/ReportServer_SQL2012? %2FTesting%2FINTest&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID= jn5ugdirg4m02nmodnm0hynq&rc%3ASchema=True" Name="INTest"> 
<Invoices1> ***need to remove*** 
    <ivhID_Collection> ***need to remove*** 
    <Invoices>...</Invoices> 
    <Invoices>...</Invoices> 
    <Invoices> 
     <Invoice> 
     <Header1> 
      <InvoiceHeader>...</InvoiceHeader> 
      <PaymentTerms>...</PaymentTerms> 
      <Dates>...</Dates> 
      <Address>...</Address> 
      <References>...</References> 
      <ChargesAllowances>...</ChargesAllowances> 
      <LineItem_Collection> ***need to remove and replace with </Header>*** 
      <LineItem> 
       <InvoiceLine>...</InvoiceLine> 
       <ProductOrItemDescription>...</ProductOrItemDescription> 
      </LineItem> 
      <LineItem> 
       <InvoiceLine>...</InvoiceLine> 
       <ProductOrItemDescription>...</ProductOrItemDescription> 
      </LineItem> 
      </LineItem_Collection> ***need to remove*** 
      <Summary>...</Summary> 
     </Header1> ***need to remove*** 
     </Invoice> 
    </Invoices> 
    <Invoices>...</Invoices> 
    <Invoices>...</Invoices> 
    <Invoices>...</Invoices> 
    /ivhID_Collection> ***need to remove*** 
</Invoices1> ***need to remove*** 
</Invoices> 

代わりにこの構造で取得しようとしています。

<Invoices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.spscommerce.com/RSX" xsi:schemaLocation="http://www.spscommerce.com/RSX http://tfl-sql01/ReportServer_SQL2012?%2FTesting%2FINDoItBest%20v7&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=jn5ugdirg4m02nmodnm0hynq&rc%3ASchema=True" Name="INDoItBest v7"> 
<Invoices>...</Invoices> 
<Invoices>...</Invoices> 
    <Invoices> 
    <Invoice> 
     <Header> 
     <InvoiceHeader>...</InvoiceHeader> 
     <PaymentTerms>...</PaymentTerms> 
     <Dates>...</Dates> 
     <Address>...</Address> 
     <References>...</References> 
     <ChargesAllowances>...</ChargesAllowances> 
     </Header> 
     <LineItem> 
     <InvoiceLine>...</InvoiceLine> 
     <ProductOrItemDescription>...</ProductOrItemDescription> 
     </LineItem> 
     <LineItem> 
     <InvoiceLine>...</InvoiceLine> 
     <ProductOrItemDescription>...</ProductOrItemDescription> 
     </LineItem> 
     <Summary>...</Summary> 
    </Invoice> 
    </Invoices> 
    <Invoices>...</Invoices> 
    <Invoices>...</Invoices> 
    <Invoices>...</Invoices> 
</Invoices> 

私はこのスタイルシートを使用して、いくつかの進歩を遂げたが、ヘッダータグと要素の名前空間の表示の再編に貼り付けています。

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:t="http://www.spscommerce.com/RSX" 
    exclude-result-prefixes="t"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<!--rule to suppress the undesired nodes--> 
<xsl:template match="t:Invoices1|t:ivhID_Collection"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="t:LineItem_Collection"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!--<xsl:template match="t:Invoice/t:Header1"> 
    <xsl:apply-templates/> 
</xsl:template>--> 

<!-- Identity Transform --> 
<xsl:template match="t:Header1"> 
    <xsl:copy> 
    <xsl:element name="Header"> 
     <xsl:apply-templates select="@*|t:InvoiceHeader|t:PaymentTerms|t:Dates|t:Address|t:References|t:ChargesAllowances"/> 
    </xsl:element> 
    <xsl:apply-templates select="@*|t:LineItem_Collection|t:Summary"/> 
    </xsl:copy> 
</xsl:template> 

<!-- Had to comment out --> 
<!--<xsl:template match="t:Invoice/t:Header1"> 
    <xsl:apply-templates/> 
</xsl:template>--> 

スタイルシートは、私が必要なもののほとんどを生産し、私はヘッダ1タグ(コードコメントアウト)を削除しようとしたときに失敗しました。また、なぜ "exclude-result-prefixes"が新しいxmlファイルから名前空間を削除しないのかを理解するのに苦労しています。

<Invoices xmlns="http://www.spscommerce.com/RSX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.spscommerce.com/RSX http://tfl-sql01/ReportServer_SQL2012?%2FTesting%2FINDoItBest%20v7&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=jn5ugdirg4m02nmodnm0hynq&rc%3ASchema=True" Name="INDoItBest v7"> 
<Invoices>...</Invoices> 
<Invoices>...</Invoices> 
<Invoices> 
    <Invoice> 
    <Header1> 
     <Header xmlns=""> 
     <InvoiceHeader xmlns="http://www.spscommerce.com/RSX">... </InvoiceHeader> 
     <PaymentTerms xmlns="http://www.spscommerce.com/RSX">... </PaymentTerms> 
     <Dates xmlns="http://www.spscommerce.com/RSX">...</Dates> 
     <Address xmlns="http://www.spscommerce.com/RSX">...</Address> 
     <References xmlns="http://www.spscommerce.com/RSX">...</References> 
     <ChargesAllowances xmlns="http://www.spscommerce.com/RSX">... </ChargesAllowances> 
     </Header> 
     <LineItem> 
     <InvoiceLine>...</InvoiceLine> 
     <ProductOrItemDescription>...</ProductOrItemDescription> 
     </LineItem> 
     <LineItem> 
     <InvoiceLine>...</InvoiceLine> 
     <ProductOrItemDescription>...</ProductOrItemDescription> 
     </LineItem> 
     <Summary> 
     <TotalAmount>756.8400</TotalAmount> 
     <TotalSalesAmount>727.1600</TotalSalesAmount> 
     <TotalLineItemNumber>2</TotalLineItemNumber> 
     </Summary> 
    </Header1> 
    </Invoice> 
</Invoices> 
<Invoices>...</Invoices> 
<Invoices>...</Invoices> 
<Invoices>...</Invoices> 
</Invoices> 

アドバイスやその他のオプションは非常に高く評価されます。

+0

'exclude-result-prefixes'はスタイルシートのリテラル結果要素の動作にのみ影響し、スタイルシートにはリテラルな結果要素はありません。私はあなたが属性の意味をその名前から推測していると思うが、それは常に危険である。 –

答えて

0

XSLTに既にt:Header1に一致するテンプレートがあるので、それに一致するテンプレートを追加しないでください.1つしか適用できません。 (あなたのケースでは、t:Invoice\t:Header1と一致するテンプレートを追加した場合、親が指定されているため、代わりにt:Header1と一致するものとして優先順位が高くなり、代わりに使用されます)。

あなたがする必要があるのは、すべてのロジックを1つのテンプレートに入れておくことです。この場合、Header1が出力ツリーにコピーされないように、そのテンプレートからxsl:copyを削除するだけです。さらに、Headerを作成するときは、名前空間ではなく、接頭辞 "t"にバインドされた名前空間で作成します。したがって、子要素はまだその名前空間に存在するため、新しい名前空間宣言が与えられます。

また
<xsl:element name="Header" namespace="http://www.spscommerce.com/RSX"> 

、あなただけ<Header>を行うことで要素を作成することができますが、デフォルトを追加する必要があります。それを行うには

一つの方法は、単純にそうように、xsl:elementに「名前空間」属性を追加していますネームスペース宣言もXSLTに渡して、正しいネームスペースに出力されるようにします。

このXSLT試してみてください:サイドノートとして

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:t="http://www.spscommerce.com/RSX" 
    xmlns="http://www.spscommerce.com/RSX" 
    exclude-result-prefixes="t"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<!--rule to suppress the undesired nodes--> 
<xsl:template match="t:Invoices1|t:ivhID_Collection"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="t:LineItem_Collection"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- Identity Transform --> 
<xsl:template match="t:Header1"> 
    <xsl:apply-templates select="@*" /> 
    <Header> 
     <xsl:apply-templates select="@*|t:InvoiceHeader|t:PaymentTerms|t:Dates|t:Address|t:References|t:ChargesAllowances"/> 
    </Header> 
    <xsl:apply-templates select="t:LineItem_Collection|t:Summary"/> 
</xsl:template> 
</xsl:stylesheet> 

を、あなたのXSLTであなたもHeader1が属性を持っていた場合、これは失敗するでしょうHeader要素

<xsl:apply-templates select="@*|t:LineItem_Collection|t:Summary"/> 

を作成した直後にこれをやっていたします子要素を作成した後に親要素に属性を追加しようとするとエラーが発生するため、コピーしたかったのです。これが私のXSLTでステートメントを2つに分割した理由です。

+0

ありがとうTim、それは素晴らしい作品です。それは本当に私を助けました。 –

関連する問題