我需要使用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&categoryName={$categoryName}&subcategoryName={$subcategoryName}" />
</head>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我得到下一个输出:
<product xmlns="" name="camera"><provider>pro</provider><model>AB200</model><color>black</color><dateOfIssue>06-01-2014</dateOfIssue><price>1000</price></product>
Run Code Online (Sandbox Code Playgroud)
但我需要没有指定名称空间的所有元素,这样的<product name="camera">..etc..</product>
建议将不胜感激
你有
<xsl:template
match="/t:categories/t:category[@name=$categoryName]/t:subcategory[@name=$subcategoryName]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:element name="product">
Run Code Online (Sandbox Code Playgroud)
这样该模板将复制t:subcategory位于名称空间中的元素,然后product在没有名称空间的内部创建元素。这样,需要添加结果树的序列化程序<product xmlns="">以确保元素按创建的顺序进行序列化。如果要在与product元素相同的名称空间中创建元素,请subcategory确保您拥有
<xsl:stylesheet xmlns="http://www.products.com" ...>
Run Code Online (Sandbox Code Playgroud)
放在样式表的根元素上(以放置在该命名空间中创建的所有元素)或使用
<xsl:element name="product" namespace="http://www.products.com">...</xsl:element>
Run Code Online (Sandbox Code Playgroud)
或只是文字
<product xmlns="http://www.products.com">...</product>
Run Code Online (Sandbox Code Playgroud)
当然,当您创建其他元素时,同样适用于它们,如果您通过第二条建议,也需要例如<xsl:element name="provider" namespace="http://www.products.com">。但是,在样式表的根元素上使用文字结果元素或正确的默认名称空间将使此操作变得更加容易。
| 归档时间: |
|
| 查看次数: |
2261 次 |
| 最近记录: |