2014-01-06 101 views
0

我需要使用xalan重定向擴展將新元素添加到現有xml文件中,但是我遇到了命名空間問題。XSLT:添加新元素時排除名稱空間

my xslt : 
<xsl:stylesheet version="1.0" 
    xmlns:redirect="http://xml.apache.org/xalan/redirect" 
    extension-element-prefixes="redirect" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:validator="xalan://com.epam.app.transformation.Validator" 
    xmlns:t="http://www.products.com" exclude-result-prefixes="#default t"> 
    <xsl:import href="addProductPage.xsl" /> 
    <xsl:param name="name"></xsl:param> 
    <xsl:param name="provider"></xsl:param> 
    <xsl:param name="model"></xsl:param> 
    <xsl:param name="color"></xsl:param> 
    <xsl:param name="dateOfIssue"></xsl:param> 
    <xsl:param name="notInStock"></xsl:param> 
    <xsl:param name="price"></xsl:param> 
    <xsl:param name="filename"></xsl:param> 
    <xsl:param name="isValid" 
     select="validator:validateFields($name, $provider, $model, $dateOfIssue, $color,$price,$notInStock)" /> 
    <xsl:param name="categoryName"></xsl:param> 
    <xsl:param name="subcategoryName"></xsl:param> 

    <xsl:output omit-xml-declaration="yes" indent="no" /> 
    <xsl:template match="/" priority="2"> 
     <html> 
      <head> 
       <title>Products Home Page</title> 
      </head> 
      <body> 
       <xsl:choose> 
        <xsl:when test="$isValid"> 
         <redirect:write select="$filename" append="false"> 

          <xsl:call-template name="saveProduct" /> 
         </redirect:write> 
         <xsl:call-template name="returnToProducts" /> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:call-template name="addProduct" /> 
              <!-- i show errors in here --> 
        </xsl:otherwise> 
       </xsl:choose> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template name="saveProduct" match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template 
     match="/t:categories/t:category[@name=$categoryName]/t:subcategory[@name=$subcategoryName]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
      <xsl:element name="product"> 
       <xsl:attribute name="name"> 
       <xsl:value-of select="$name" /> 
       </xsl:attribute> 
       <xsl:element name="provider"> 
        <xsl:value-of select="$provider" /> 
       </xsl:element> 
       <xsl:element name="model"> 
        <xsl:value-of select="$model" /> 
       </xsl:element> 
       <xsl:element name="color"> 
        <xsl:value-of select="$color" /> 
       </xsl:element> 
       <xsl:element name="dateOfIssue"> 
        <xsl:value-of select="$dateOfIssue" /> 
       </xsl:element> 
       <xsl:choose> 
        <xsl:when test="$notInStock"> 
         <xsl:element name="notInStock" /> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:element name="price"> 
          <xsl:value-of select="$price" /> 
         </xsl:element> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:element> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template name="returnToProducts"> 
     <html> 
      <head> 
       <meta http-equiv="refresh" 
        content="0;url=controller?command=GetProductsCmd&amp;categoryName={$categoryName}&amp;subcategoryName={$subcategoryName}" /> 
      </head> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

我得到下一個輸出中:

<product xmlns="" name="camera"><provider>pro</provider><model>AB200</model><color>black</color><dateOfIssue>06-01-2014</dateOfIssue><price>1000</price></product> 

,但我需要沒有指定命名空間的所有元素這樣<product name="camera">..etc..</product> 任何建議,將不勝感激

+0

您能否包含您正在使用的_input_ XML示例?我懷疑這個輸入有一個'xmlns =「東西」'那正在阻礙你。 –

回答

3

你有

<xsl:template 
     match="/t:categories/t:category[@name=$categoryName]/t:subcategory[@name=$subcategoryName]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
      <xsl:element name="product"> 

所以該模板將複製t:subcategory元素位於名稱空間中,但在沒有名稱空間中創建了一個product元素。這樣,結果樹的序列化程序需要添加<product xmlns="">以確保元素序列化爲創建。如果你想創建在相同的命名空間subcategory元素product元素然後確保你要麼有

<xsl:stylesheet xmlns="http://www.products.com" ...> 
樣式表的根元素

(把該命名空間創建的所有元素),或使用

<xsl:element name="product" namespace="http://www.products.com">...</xsl:element> 

或只是一個文字

<product xmlns="http://www.products.com">...</product> 

當然,你創建的其他元素也同樣適用於他們,如果你去通過第二個建議你需要<xsl:element name="provider" namespace="http://www.products.com">。但是在樣式表的根元素上使用文字結果元素或者甚至是正確的默認名稱空間使得它更容易。

+0

感謝您的詳細解釋 – DeadKennedy