使用XSLT删除多个XML节点

时间:2019-05-09 09:28:18

标签: xslt

我正在尝试从XML文件中删除2个根节点,但似乎无法正确处理。我试图删除第一部分,但无济于事。

我希望文件是这样的:

 [{                
    "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"
}]

但是它来了:

 {        
    "ns1:ZZMRP_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"
}] }}

我需要删除的部分"ns1:ZZMRP_Parts" : { "Part" :[{

来自评论:输入示例

<ns1:DDMRP_Parts xmlns:ns1="urn:za.xxxxxxxx.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 &amp; 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 &amp; Mixing Eq</MaterialType>
  </Part>
</ns1:DDMRP_Parts>

1 个答案:

答案 0 :(得分:0)

XSLT 3可以处理JSON,并且使用XPath 3.1,您可以轻松地仅选择感兴趣的更复杂的输入JSON所感兴趣的部分:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

    <xsl:output method="json" indent="yes"/>

    <xsl:template match="json">
        <xsl:sequence select="parse-json(.)('ns1:ZZMRP_Parts')?Part"/>
    </xsl:template>

</xsl:stylesheet>

假设输入为

<?xml version="1.0" encoding="UTF-8"?>
<json><![CDATA[
{        
    "ns1:ZZMRP_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"
}] }}
]]></json>

https://xsltfiddle.liberty-development.net/bFN1y9B