我的输入xml与json名称空间一样..
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<json:object name="Login">
<json:object name="Group">
<json:object name="TargetSystem">
<json:string name="Name">john</json:string>
<json:string name="Password"/>
</json:object>
</json:object>
</json:object>
</json:object>
我需要像这样的输出
<Login>
<Group>
<TargetSystem>
<Name>john</Name>
<Password/>
</TargetSystem>
</Group>
</Login>
我必须使用xslt在输入xml中使用属性名称值创建此xml。 我使用下面的xslt。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/">
<xsl:apply-templates select="json:object/*"/>
</xsl:template>
<xsl:template match="json:object">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
使用这个我只得到约翰。 我必须使用一些循环的概念.. 有谁可以帮助我,我怎么能实现这个目标?
答案 0 :(得分:1)
如果你的XSLT是最小的,那么可以提出一个适用于任何命名空间的通用XSLT。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="*[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
换句话说,这会匹配任何带有 @name 属性的元素,而是创建该名称的元素。由于根元素没有这样的属性,因此不会输出,但默认模板匹配将继续处理其子元素。
答案 1 :(得分:0)
我想你想要
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
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:object[@name]">
<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>