【问题标题】:Transformation with namespace使用命名空间进行转换
【发布时间】: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


【解决方案1】:

如果你真的想生成一个没有在任何元素或属性名称中实际使用的命名空间声明,并且它没有出现在源文档中,你可以使用xsl:namespace 指令(一个相当专业的指令,仅出于这个晦涩的目的而存在)。

【讨论】:

  • 当我提出这样的建议时,我假设您会去您最喜欢的 XSLT 教科书并查找它——我不打算提供教程。你可以像 xsl:attribute 一样使用它。
猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多