2016-05-06 7 views
1

私は私のエラーはここに私のスタイルシートを使用して、現在ここで全くわからない: :不要なノード

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet exclude-result-prefixes="exsl xs" 
extension-element-prefixes="exsl" version="2.0" 
xmlns:exsl="http://exslt.org/common" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <!--xsl:import-schema schema-location="ProductFeed5.6.xsd"/--> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8" 
     byte-order-mark="no" omit-xml-declaration="no" 
     xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6" /> 

    <xsl:strip-space elements="*" /> 

    <!-- Parameter to determine when using 'contoursbaby' or 'kolcraft' feed 
     values --> 
    <xsl:param name="feedName" as="xs:string" /> 

    <!-- copy all nodes that do not match a given template --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/*"> 
     <xsl:choose> 
      <xsl:when test="starts-with(name(), 'dataroot_')"> 
       <xsl:element name="Feed" namespace="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6"> 
        <xsl:attribute name="name" select="$feedName" /> 
        <xsl:attribute name="incremental" select="'false'" /> 
        <xsl:attribute name="extractDate"><xsl:value-of select='current-dateTime()' /></xsl:attribute> 
        <xsl:if test="not(empty($feedName))"> 
         <xsl:choose> 
          <xsl:when test="$feedName eq 'kolcraft'"> 
           <xsl:copy-of select="exsl:node-set(document('kolcraft-brands.xml'))" /> 
           <xsl:copy-of select="exsl:node-set(document('kolcraft-categories.xml'))" /> 
          </xsl:when> 
          <xsl:otherwise> 
           <xsl:value-of select="document('contours-categories.xml')" /> 
          </xsl:otherwise> 
         </xsl:choose> 
         <xsl:copy> 
          <xsl:apply-templates select="recordset" /> 
         </xsl:copy> 
        </xsl:if> 
       </xsl:element> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="recordset"> 
     <Products> 
      <xsl:for-each select="row"> 
       <Product> 
        <xsl:attribute name="removed" select="'false'" /> 
        <xsl:apply-templates select="@*|node()" /> 
       </Product> 
      </xsl:for-each> 
     </Products> 
    </xsl:template> 

    <xsl:template match="F1"> 
     <ExternalId><xsl:value-of select="." /></ExternalId> 
    </xsl:template> 

    <xsl:template match="F2"> 
     <UPCs> 
      <UPC><xsl:value-of select="." /></UPC> 
     </UPCs> 
    </xsl:template> 

    <xsl:template match="F3"> 
     <Name><xsl:value-of select="." /></Name> 
    </xsl:template> 

    <xsl:template match="F4"> 
     <Description><xsl:value-of select="." /></Description> 
    </xsl:template> 

    <xsl:template match="F5"> 
     <ProductPageUrl><xsl:value-of select="." /></ProductPageUrl> 
    </xsl:template> 

    <xsl:template match="F6"> 
     <ImageUrl><xsl:value-of select="." /></ImageUrl> 
    </xsl:template> 

    <xsl:template match="F7"> 
     <CategoryExternalId><xsl:value-of select="lower-case(normalize-space(replace(., '[/ ]', '-')))" /></CategoryExternalId> 
    </xsl:template> 

    <xsl:template match="fielddescription" /> 

</xsl:stylesheet> 

、彼らが起こる理由を私は持っていると私はわからないんだけど2つの問題があります<Brands> & <Categories>は、インポート時に空の名前空間を取得しています。は、ルート<Feed>の最初の子として引き続き表示されます。ここで

は、その使用して、ソースドキュメントの種類の抜粋です:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<dataroot_r00295> 

    <fielddescription> 
     <F1>ExternalID</F1> 
     <F2>UPC</F2> 
     <F3>Name</F3> 
     <F4>Description</F4> 
     <F5>ProductPageUrl</F5> 
     <F6>ProductImageUrl</F6> 
     <F7>CategoryExternalID</F7> 
    </fielddescription> 
    <recordset> 
    <row> 
      <F1>ED003-QCX</F1>  
      <F2>031878025147</F2>  
      <F3>Sealy Naturals-Cotton Crib Mattress Pad</F3>  
      <F4>Give baby plush comfort from natural cotton fibers with the innovative Sealy Naturals-Cotton Crib Mattress Pad.</F4>  
      <F5>http://www.kolcraft.com/sealy-naturals-cotton-crib-mattress-pad.html</F5>  
      <F6>http://www.kolcraft.com/media/catalog/product/e/d/ed003-qcx-1_1_4.jpg</F6>  
      <F7>Bedding/Pads</F7>  
    </row> 
    </recordset> 
</dataroot_r00295> 

そして、ここではそれが生成するものの抜粋です:

<?xml version="1.0" encoding="UTF-8"?> 
<Feed extractDate="2016-05-05T19:32:27.863-05:00" 
     incremental="false" 
     name="kolcraft" 
     xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6"> 
     <Brands xmlns=""> 
      <Brand removed="false"> 
       <ExternalId>brands</ExternalId> 
       <Name>Brands</Name> 
       <!--BrandPageUrl>http://www.kolcraft.com/brands.html</BrandPageUrl --> 
      </Brand> 
     </Brands> 
     <Categories xmlns=""> 
      <Category removed="false"> 
       <ExternalId>baby-products</ExternalId> 
       <Name>Baby Products</Name> 
       <CategoryPageUrl>http://www.kolcraft.com/baby-products.html</CategoryPageUrl> 
      </Category> 
     </Categories> 
     <dataroot_r00295 xmlns=""> 
      <Products> 
       <Product removed="false"> 
        <ExternalId>ED003-QCX</ExternalId> 
        <UPCs> 
         <UPC>031878025147</UPC> 
        </UPCs> 
        <Name>Sealy Naturals-Cotton Crib Mattress Pad</Name> 
        <Description> 
         Give baby plush comfort from natural cotton fibers with the innovative Sealy Naturals-Cotton 
         Crib Mattress Pad. 
        </Description> 
        <ProductPageUrl>http://www.kolcraft.com/sealy-naturals-cotton-crib-mattress-pad.html</ProductPageUrl> 
        <ImageUrl>http://www.kolcraft.com/media/catalog/product/e/d/ed003-qcx-1_1_4.jpg</ImageUrl> 
        <CategoryExternalId>bedding-pads</CategoryExternalId> 
       </Product> 
      </Products> 
     </dataroot_r00295> 
</Feed> 

誰もが私のスタイルシートで間違って何が起こっているのか説明できますか?私は困惑している。

その他:テンプレートをより効率的に使用する方法がある場合は、XSLTを使用する適切な方法を教えてください。

+0

を持っていればよいですここで ''と ''は ' 'のように名前空間が定義されていません。 – Parfait

+0

これらの文書に含まれるもののスニペットを追加しました。 「」と「」はこれらの包含物の一部です。 –

答えて

3

一般に、結果要素が特定の名前空間内にあると想定されるXML文書を作成する場合は、その名前空間の名前空間宣言をそれぞれxsl:stylesheetに入れます。xsl:transformルート要素スタイルシートの、異なるテンプレートで作成すなわち

<xsl:stylesheet 
    exclude-result-prefixes="exsl xs" 
    version="2.0" 
    xmlns:exsl="http://exslt.org/common" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6"> 

そのように、すべての結果の要素は、その名前空間になり、あなたが特定のテンプレートの内部を変更する必要がある場合でも、あなたはそこxmlns=""を置くことができます。

xmlns="''がコピーされていない場合は、xmlns="''が表示されます。他のドキュメントからコピーした要素が名前空間にない場合、名前空間http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6あなたはそれらをコピーすることはできません、代わりにあなたはそれらを使用して変換する必要がありますあなたは(xsl:stylesheetxmlns="..."と)私の以前の提案を想定し、名前空間内の要素を作成するテンプレートを設定<xsl:apply-templates select="document('foo.xml')/node()"/>それはあなたがそうである、我々はここで見ることができない他の文書からのデータを引っ張っている

<xsl:template match="*"> 
    <xsl:element name="{local-name()}"> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:element> 
</xsl:template> 
+0

私は実際に2番目の問題を修正しました。これですべてが解決されます:)ありがとう!私はそれが輸入がどのように働いたか分からなかった –