如何从XML转换时使用XSLT JSON删除根节点

问题描述:

{ “根”:{ “u_effective_date”: “2017年3月27日”, “u_name”: “测试”, “u_preferred_name”: “测试”, “u_title”: “迪尔人力资源”, “u_department”: “人力资源” }}如何从XML转换时使用XSLT JSON删除根节点

需要的输出: { “u_effective_date”:“2017-03 -27“, ”u_name“:”Test“, ”u_preferred_name“:”Test“, “u_title”: “迪尔人力资源”, “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

XSLT转换XML不JSON。使用任何通用语言(Java,PHP,Python等)来读取JSON并重新映射字典/列表项目。 – Parfait

+0

下面是我试图转换成Json格式的xml文件 xmlns: etv =“urn:com.workday/etv” xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance” xmlns:ws =“urn:com.workday/workersync”> 2017年3月27日 u_effective_date> 测试测试 u_preferred_name> 迪尔人力资源人力资源 u_department> –

+0

嗨芭菲,谢谢您回复..我用xml和xslt更新了这个问题。我应该编辑什么来跳过xml中的根节点并从子节点输出。 –

只需将*模板匹配从"/"更改为"/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" 
} 
+0

谢谢冻糕! –

+0

没问题!乐意效劳。不要忘记在*上说[谢谢](https://meta.stackexchange.com/a/5235)的特殊方式。 – Parfait