【发布时间】:2014-09-22 21:06:03
【问题描述】:
InputXML:
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<json:object name="Customers">
<json:array name="Order">
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password">Doe</json:string>
</json:object>
<json:object>
<json:string name="Name">Adam</json:string>
<json:string name="Password">eve</json:string>
</json:object>
</json:array>
</json:object>
</json:object>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:tns="http://some-other-namespace" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" exclude-result-prefixes="json">
<xsl:template match="/json:object">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="json:array[@name]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="json:object[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="json:object">
<xsl:element name="{../@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="json:string[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
实际输出:
<Customers>
<Order>
<Name>john</Name>
<Password>Doe</Password>
</Order>
<Order>
<Name>Adam</Name>
<Password>eve</Password>
</Order>
</Customers>
所需的输出:
<Customers xmlns:tns="http://some-other-namespace">
<Order>
<Name>john</Name>
<Password>Doe</Password>
</Order>
<Order>
<Name>Adam</Name>
<Password>eve</Password>
</Order>
</Customers>
我知道我们可以通过将实际输出作为其输入来在另一个 xsl 中进行身份转换并获得我想要的输出。
但我想在一个样式表中完成所有操作。如何使用单个 xsl 实现这一目标
【问题讨论】:
-
您的预期结果毫无意义。它与实际输出之间的唯一区别是 unused 命名空间声明。它不应该对接收应用程序产生任何影响。
-
我同意。但我想用它来进行模式验证
标签: xml xslt xslt-1.0 xslt-2.0