2016-12-30 80 views
0

新手访问此站点并使用xslt,但遇到了一个障碍,将SSRS 2008v2呈现的xml文件转换为另一个第三方EDI转换的XSL原始格式。我一直在搜索这个网站和其他一些,但现在挣扎着把它放在一起。我从下面的原始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>--> 

样式表产生了我所需要的大部分内容,但是当我试图去除Header1标签(代码注释掉)时失败了。另外,努力去理解为什么“排除结果前缀”不适用于从新的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的模板,因此您不应再添加一个匹配它的模板,因为只有其中一个可以应用。 (在你的情况下,如果你确实添加了一个匹配t:Invoice\t:Header1的模板,那么由于指定了父项,它将具有更高的优先级,因为它只是匹配t:Header1并被替代使用)。

你需要做的是把所有的逻辑放在单个模板中。在这种情况下,您只需从该模板中删除xsl:copy以避免Header1被复制到输出树中。此外,当您创建Header时,您将在没有命名空间中创建它,而不是在绑定到前缀“t”的名称空间中创建它。因此,子元素将被赋予新的名称空间声明,因为它们仍将位于该名称空间中。

一种方式做到这一点是只需要添加一个“命名空间”属性为xsl:element,像这样:

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

或者,您也可以通过只是在做<Header>创建元素,但你将需要添加默认命名空间声明到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你也马上创建Header元素

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

后这样做,如果Header1有属性,这会令你失望想要复制,因为在创建子元素后尝试向父元素添加属性是个错误。这就是为什么在我的XSLT中,我将语句分成两部分。

+0

谢谢蒂姆,它工作得很好。这真的帮了我。 –