我需要使用XSLT将XML文件重新格式化为JSON,然后删除标题标签并插入数组]括号 [英] I need to reformat a XML file to JSON using XSLT and remove the header tags and insert the array ] brackets
问题描述
我需要将XML源转换为指定的JSON格式.为此,我需要删除头节点,保留数组主体并将其封装在[]中.我已经转换了正文,但是在删除标题节点并插入封装的[]
时遇到了麻烦这是我收到的XML格式:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:DDMRP_Parts xmlns:ns1="urn:za.sabmiller.com:supplychain:3rdp:transdata">
<Part>
<PartNumber>000096</PartNumber>
<Location>A000</Location>
<Description>TEST OF RAMIS</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
<PartType>1</PartType>
<FixedLeadTime>1</FixedLeadTime>
<MaterialType>Filling & Mixing Eq</MaterialType>
</Part>
<Part>
<PartNumber>000096</PartNumber>
<Location>A000</Location>
<Description>TEST OF RAMIS</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
<PartType>1</PartType>
<FixedLeadTime>1</FixedLeadTime>
<MaterialType>Filling & Mixing Eq</MaterialType>
</Part>
</ns1:DDMRP_Parts>
我尝试使用SAP PI上提供的适配器进行转换,但这不能满足整个阵列主体的要求.我尝试过使用XSLT重新格式化,但是移除外部节点并用[]封装并不能很好地解决问题
这是我的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<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>
这是我目前正在生产的:
{
"ns1:DDMRP_Parts" : { "Part" :[{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
},{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
}] }}
这是我需要输出的:
[{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
},{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
}]
我建议您这样尝试:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<!-- array -->
<xsl:text>[ </xsl:text>
<xsl:apply-templates select="*"/>
<xsl:text> ]</xsl:text>
</xsl:template>
<xsl:template match="*">
<!-- object -->
<xsl:text>	{ </xsl:text>
<xsl:apply-templates select="*" mode="data"/>
<xsl:text> 	}</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="data">
<!-- name/value pair -->
<xsl:text>		"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>" : "</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
P.S.空格字符是可选的,可以删除而不会影响JSON结果的有效性.
I need to convert an XML source to a specified JSON format. To do this i need to remove header nodes, retain the array body and encapsulate in [ ]. I have converted the body but i am having trouble removing the header nodes and inserting the encapsulating [ ]
This is the XML format I receive:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:DDMRP_Parts xmlns:ns1="urn:za.sabmiller.com:supplychain:3rdp:transdata">
<Part>
<PartNumber>000096</PartNumber>
<Location>A000</Location>
<Description>TEST OF RAMIS</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
<PartType>1</PartType>
<FixedLeadTime>1</FixedLeadTime>
<MaterialType>Filling & Mixing Eq</MaterialType>
</Part>
<Part>
<PartNumber>000096</PartNumber>
<Location>A000</Location>
<Description>TEST OF RAMIS</Description>
<UnitOfMeasure>EA</UnitOfMeasure>
<PartType>1</PartType>
<FixedLeadTime>1</FixedLeadTime>
<MaterialType>Filling & Mixing Eq</MaterialType>
</Part>
</ns1:DDMRP_Parts>
I have tried using the adapter provided on SAP PI to do the conversion but this does not cater for a full array body. I have tried to reformat using XSLT but I am not coming right with removing the outer nodes and encapsulating with [ ]
This is my XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<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>
This is what i am currently producing:
{
"ns1:DDMRP_Parts" : { "Part" :[{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
},{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
}] }}
This is what i need to output:
[{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
},{
"PartNumber" : "000096",
"Location" : "A000",
"Description" : "TEST OF RAMIS",
"UnitOfMeasure" : "EA",
"PartType" : "1",
"FixedLeadTime" : "1",
"MaterialType" : "Filling & Mixing Eq"
}]
I suggest you try it this way:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<!-- array -->
<xsl:text>[ </xsl:text>
<xsl:apply-templates select="*"/>
<xsl:text> ]</xsl:text>
</xsl:template>
<xsl:template match="*">
<!-- object -->
<xsl:text>	{ </xsl:text>
<xsl:apply-templates select="*" mode="data"/>
<xsl:text> 	}</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="data">
<!-- name/value pair -->
<xsl:text>		"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>" : "</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
P.S. The whitespace characters are optional and can be removed without affecting the validity of the JSON result.
这篇关于我需要使用XSLT将XML文件重新格式化为JSON,然后删除标题标签并插入数组]括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!