{" Root":{ " u_effective_date":" 2017-03-27", " u_name":"测试", " u_preferred_name":"测试", " u_title":" Dir Human Resources", " u_department":"人力资源" }}
到
所需输出: { " u_effective_date":" 2017-03-27", " u_name":"测试", " u_preferred_name":"测试", " u_title":" Dir Human Resources", " u_department":"人力资源" }
这是xml:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xtt="urn:com.workday/xtt"
xmlns:etv="urn:com.workday/etv"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="urn:com.workday/workersync">
<u_effective_date>2017-03-27</u_effective_date>
<u_name>Test</u_name>
<u_preferred_name>Test</u_preferred_name>
<u_title>Dir Human Resources</u_title>
<u_department>Human Resources</u_department>
</Root>
以下是我正在使用的xslt:
<?xml version="1.0"?>
<xsl:template match="/">{
<xsl:apply-templates select="*"/>}
</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*">
"<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
</xsl:template>
<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>
<!-- Object Properties -->
<xsl:template name="Properties">
<xsl:variable name="childName" select="name(*[1])"/>
<xsl:choose>
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
<xsl:otherwise>{
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*"/>
}</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
答案 0 :(得分:0)
只需将顶级模板匹配从"/"
更改为"/Root"
即可。就是这样!
<强> XSLT 强>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/Root">{
<xsl:apply-templates select="*"/>
}
</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*">
"<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
</xsl:template>
<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>
<!-- Object Properties -->
<xsl:template name="Properties">
<xsl:variable name="childName" select="name(*[1])"/>
<xsl:choose>
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
<xsl:otherwise>{
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*"/>
}</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>
输出
{
"u_effective_date" : "2017-03-27",
"u_name" : "Test",
"u_preferred_name" : "Test",
"u_title" : "Dir Human Resources",
"u_department" : "Human Resources"
}