xmlns名称空间无法在XSLT中正确读取当前未转换文件的问题

时间:2013-12-19 15:42:07

标签: xml xslt-1.0

我已经为SSIS创建了一个XSLT,以根据客户端requement将XML转换为XML。 我不太擅长使用网络帮助来解决这个问题的XSLT。

现在我的当前XLST工作正常,如果我删除xmlns =“http://www.ofdaxml.org/schema”>线 但我正在寻找自动修复因为我每次都无法进行手工操作和来自clint的来源所以我无法在源文件中进行更改。

所以我只想修复XSLT文件

任何人都可以帮助我吗?

当前源文件

<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.ofdaxml.org/schema C:\PROGRA~1    \HMI\XML\OFDAOrder_03_00_00.xsd" xmlns="http://www.ofdaxml.org/schema"     SchemaVersion="03.00.00">
  <Header>
    <Vendor>
      <Enterprise>
        <Code>www.hermanmiller.com</Code>
      </Enterprise>
      <Code>CHM</Code>
    </Vendor>
    <Language>en-US</Language>
    <Currency>CAD</Currency>
  </Header>
  <PurchaseOrder>
    <Project>
      <Title>Imp0.spe</Title>
      <SpecifierFile>
        <Name>W:\Killer\Gwen\E1\It110.xml</Name>
        <SpecifierSoftware Version="104.00">HM specIT</SpecifierSoftware>
      </SpecifierFile>
      <BusinessSoftware>HM specIT version 104.00</BusinessSoftware>
    </Project>
    <Header>
      <PONumber>IT46_Room46110.spe</PONumber>
      <TransactionCode>New</TransactionCode>
      <CreatedDate>2013-11-29</CreatedDate>
      <RequestedDate Type="DeliverOnOrBefore">2011-07-15</RequestedDate>
      <BillType>Dealer</BillType>
      <OrderType>99</OrderType>
      <Contract>
        <Code>CG1615</Code>
        <Description>EP
</Description>
        <SalesVolume Currency="INR">0</SalesVolume>
      </Contract>
    </Header>
    <OrderLineItem>
      <VendorRef EnterpriseRef="www.HM.com">HMI</VendorRef>
      <Status>Complete</Status>
      <Quantity>1</Quantity>
      <LineItemNumber>2</LineItemNumber>
      <SpecItem>
        <Number>46-3620-69</Number>
        <Description>+Stg Case Std Pull 36W 20D CaseHgt 65 5/8H</Description>
        <Catalog>
          <Code>MFS</Code>
        </Catalog>
        <Option Sequence="1">
          <Code>XS</Code>
          <Description>+textured paint on smooth steel</Description>
        </Option>
        <Option Sequence="2">
          <Code>G1</Code>
          <Description>+graphite</Description>
        </Option>
        <Option Sequence="6">
          <Code>B1</Code>
          <Description>+1 -high base</Description>
        </Option>
      </SpecItem>
    </OrderLineItem>
  </PurchaseOrder>
</Envelope>

当前的XSLT文件

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.ofdaxml.org/schema"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/Envelope"> 
<PurchaseOrder> 
<xsl:for-each select="PurchaseOrder/OrderLineItem"> 
<OrderLineDetails> 
<VendorRef><xsl:value-of select="VendorRef"/></VendorRef> 
<LineItemNumber><xsl:value-of select="LineItemNumber"/></LineItemNumber> 
<SpecItemNumber><xsl:value-of select="SpecItem/Number"/></SpecItemNumber> 
<SpecItemDesc><xsl:value-of select="SpecItem/Description"/></SpecItemDesc> 
<ITMOptionCode><xsl:value-of select="SpecItem/Option/Code"/></ITMOptionCode> 
<ITMOptionSeq><xsl:value-of select="SpecItem/Option/@Sequence"/></ITMOptionSeq> 
<ITMOptionDesc><xsl:value-of select="SpecItem/Option/Description"/></ITMOptionDesc> 
<ITMOptionCodeDescOld><xsl:value-of select="concat                (' ',SpecItem/Option/Code, ' ',SpecItem/Option/Description)"/></ITMOptionCodeDescOld> 
<ITMOptionCodeDesc>
  <xsl:for-each select="SpecItem/Option">
    <xsl:if test="position() > 1">
      <xsl:text> - </xsl:text>
    </xsl:if>
    <xsl:value-of select="concat(@Sequence, ' - ',Code, ' - ',Description)"/>
  </xsl:for-each>
</ITMOptionCodeDesc> 


</OrderLineDetails> 
</xsl:for-each> 
</PurchaseOrder> 
</xsl:template> 
</xsl:stylesheet>

我已经更改了我的XSLT文件但仍然收到错误。

当前的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:ofda="http://www.ofdaxml.org/schema"
 xmlns="http://www.ofdaxml.org/schema" exclude-result-prefixes="ofda">
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/Envelope"> 
<PurchaseOrder> 
<xsl:for-each select="ofda:PurchaseOrder/OrderLineItem"> 
<OrderLineDetails> 
<VendorRef><xsl:value-of select="ofda:VendorRef"/></VendorRef> 
<LineItemNumber><xsl:value-of select="ofda:LineItemNumber"/></LineItemNumber> 
<SpecItemNumber><xsl:value-of select="ofda:SpecItem/Number"/></SpecItemNumber> 
<SpecItemDesc><xsl:value-of select="ofda:SpecItem/Description"/></SpecItemDesc> 
<ITMOptionCode><xsl:value-of select="ofda:SpecItem/Option/Code"/></ITMOptionCode> 
<ITMOptionSeq><xsl:value-of select="ofda:SpecItem/Option/@Sequence"/></ITMOptionSeq> 
<ITMOptionDesc><xsl:value-of     select="ofda:SpecItem/Option/Description"/></ITMOptionDesc> 
<ITMOptionCodeDescOld><xsl:value-of select="concat                (' ',ofda:SpecItem/Option/Code, ' ',ofda:SpecItem/Option/Description)"/></ITMOptionCodeDescOld> 
<ITMOptionCodeDesc>
  <xsl:for-each select="ofda:SpecItem/Option">
    <xsl:if test="position() > 1">
      <xsl:text> - </xsl:text>
    </xsl:if>
    <xsl:value-of select="concat(@Sequence, ' - ',ofda:Code, ' - ',ofda:Description)"/>
  </xsl:for-each>
</ITMOptionCodeDesc> 

</OrderLineDetails> 
</xsl:for-each> 
</PurchaseOrder> 
</xsl:template> 
</xsl:stylesheet>

当前输出

<?xml version="1.0" encoding="utf-8"?>www.hr.comCHMen-USCADImperialOil_Trailer46_Room46110.speW:\hr\Gwen\E1\ImperialOil_Trailer46110.xmlHMspecITHMspecIT version 104.00ImperialOil_Trailer46_Room46110.speNew2013-11-292011-07-15Dealer99CG1615Exxon Mobil Global Svcs.Co.-CANADA0HMIComplete1160.0060.0018.0018.0015.0015.00FT75.0070.00FT199.+Cable Management TroughHFTHMIComplete121157.001157.00557.93557.93428.09428.09MS63.0051.77846-3620-69+Stg Case Std Pull 36W 20D CaseHgt 65 5/8HMFSXS+textured paint on smooth steelG1+graphiteW+coat rod with 3 shelves (only available with T2 top)KD+keyed differently  blackT2+1 1/4 -high painted metal top with squared edgeB1+1 -high baseHMIComplete131844.001844.00922.00922.00737.60737.60MF605026-3620-4+Lateral File Std Pull 4-High 36W 20DMERXS+textured paint on smooth steelG1+graphiteT1+1 -high painted metal top with squared edgeOH+overhead hinged door 26-1/4NS1+1 shelf totalKD+keyed differently  blackE+fixed front 13-1/89R+side-to-side filing railKD+keyed differently  blackE+fixed front 13-1/89R+side-to-side filing railE+fixed front 13-1/89R+side-to-side filing railCB+counterweightB1+Base 1 in HHMIComplete14626.00626.00194.06194.06162.76162.76UP74.0069.00LW120.20BF+Mobile Pedastool W-Pull 20D B/FHTUSR+3/4-extension roller slides on box drawers  full-extension ball bearing on file drawerXS+textured paint on smooth steelG1+graphite8T+crossing-Pr Cat 118+crossing indigoH1+hand gripKD+keyed differently  black5M+pencil tray in box drawer  2 file converters in file drawerHMIComplete152485.712485.711491.431491.43745.71745.71FT7040NXLR6630E1S+Work Surf Sq-Edge Rectangular Lam 30D 66WBKR28+canyonEdgeWNBaseblackHMIComplete16757.00757.00319.45319.45267.22267.22ZZ64.7057.80Workrite monitor armMonitor arm with extended arm WRT0I@silverHMIComplete17440.00440.00440.00440.00308.00308.00300InstallationInstallation ChargeCHA

我仍然遇到同样的问题我不知道我缺少什么部分更新文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:ofda="http://www.ofdaxml.org/schema"
 xmlns="http://www.ofdaxml.org/schema" exclude-result-prefixes="ofda">
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="ofda:Enveloper"> 

<PurchaseOrder> 
<xsl:for-each select="ofda:PurchaseOrder/OrderLineItem"> 
<OrderLineDetails> 
<VendorRef><xsl:value-of select="ofda:VendorRef"/></VendorRef> 
<LineItemNumber><xsl:value-of select="ofda:LineItemNumber"/></LineItemNumber> 
<SpecItemNumber><xsl:value-of select="ofda:SpecItem/Number"/></SpecItemNumber> 
<SpecItemDesc><xsl:value-of select="ofda:SpecItem/Description"/></SpecItemDesc> 
<ITMOptionCode><xsl:value-of select="ofda:SpecItem/Option/Code"/></ITMOptionCode> 
<ITMOptionSeq><xsl:value-of select="ofda:SpecItem/Option/@Sequence"/></ITMOptionSeq> 
<ITMOptionDesc><xsl:value-of     select="ofda:SpecItem/Option/Description"/></ITMOptionDesc> 
<ITMOptionCodeDescOld><xsl:value-of select="concat            (' ',ofda:SpecItem/Option/Code, ' ',ofda:SpecItem/Option/Description)"/></ITMOptionCodeDescOld> 
<ITMOptionCodeDesc>
      <xsl:for-each select="ofda:SpecItem/Option">
        <xsl:if test="position() > 1">
      <xsl:text> - </xsl:text>
    </xsl:if>
    <xsl:value-of select="concat(@Sequence, ' - ',ofda:Code, ' - ',ofda:Description)"/>
  </xsl:for-each>
</ITMOptionCodeDesc> 

</OrderLineDetails> 
</xsl:for-each> 
</PurchaseOrder> 
</xsl:template> 
    </xsl:stylesheet>

请让我知道我必须改变的地方

1 个答案:

答案 0 :(得分:1)

您应该阅读一些有关XML / XSLT和命名空间的文章。

您的输入XML具有声明为xmlns="http://www.ofdaxml.org/schema"的默认命名空间。这意味着所有未加前缀的元素都属于此命名空间。

因此,您还应该在XSLT中声明命名空间。优先使用前缀。您现在只将其声明为默认命名空间,但前缀为。

更改

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.ofdaxml.org/schema">

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ofda="http://www.ofdaxml.org/schema" xmlns="http://www.ofdaxml.org/schema" exclude-result-prefixes="pfda">

名称空间的上述声明意味着:

  • 输入XML中的所有未加前缀的元素都与XSLT中的前缀元素进行匹配。您可以使用此前缀从输入XML中选择元素,例如odfa:Envelope
  • XSLT中的默认命名空间适用于您在XSLT中创建的所有新元素。 注意:您可以省略默认值,但是您应该为所有新创建的元素添加前缀。

exclude-result-prefixes="odfa"不会将命名空间输出到结果XML中。现在您使用前缀声明了名称空间,您可以使用此前缀选择属于此名称空间的节点,如下所示:

<xsl:template match="/odfa:Envelope"> 
<xsl:for-each select="odfa:PurchaseOrder/odfa:OrderLineItem"> 
<VendorRef><xsl:value-of select="odfa:VendorRef"/></VendorRef>