【问题标题】:XSLT: exclude namespace when adding new elementsXSLT:添加新元素时排除命名空间
【发布时间】:2014-01-06 10:10:18
【问题描述】:

我需要使用 xalan 重定向扩展在现有 xml 文件中添加一个新元素,但我遇到了命名空间的问题。

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>

但我需要所有没有指定命名空间的元素,例如 &lt;product name="camera"&gt;..etc..&lt;/product&gt; 任何建议将不胜感激

【问题讨论】:

  • 您能否提供一个您正在使用的 input XML 示例?我怀疑输入中有一个xmlns="something" 会妨碍您。

标签: xml xslt xalan


【解决方案1】:

你有

<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 元素。这样,结果树的序列化程序需要添加&lt;product xmlns=""&gt; 以确保元素在创建时被序列化。如果您想在与 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>

当然,当您创建其他元素时,同样适用于它们,如果您遵循第二个建议,您需要例如&lt;xsl:element name="provider" namespace="http://www.products.com"&gt; 也是。但是在样式表的根元素上使用文字结果元素甚至是正确的默认命名空间会更容易。

【讨论】:

    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    相关资源
    最近更新 更多