XSLT for XML具有相同的命名空间名称,不同的URI

时间:2018-01-16 22:10:14

标签: xml xslt xpath azure-logic-apps edi

我有以下XML文档,由Azure Logic Apps从EDI 856解码:

<ins0:X12InterchangeXml DelimiterSetSerializedData="13:10:-1:42:60:-1:-1:-1:-1" 
xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006/InterchangeXML">
<ns0:ISA 
    xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">
    <ISA01>00</ISA01>
    <ISA02></ISA02>
    <ISA03>01</ISA03>
    <ISA04></ISA04>
    <ISA05>ZZ</ISA05>
    <ISA06>123456789      </ISA06>
    <ISA07>12</ISA07>
    <ISA08>9987654321     </ISA08>
    <ISA09>180102</ISA09>
    <ISA10>2108</ISA10>
    <ISA11>U</ISA11>
    <ISA12>00400</ISA12>
    <ISA13>000000351</ISA13>
    <ISA14>0</ISA14>
    <ISA15>P</ISA15>
    <ISA16>&lt;</ISA16>
</ns0:ISA>
<FunctionalGroup DocType="http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_856">
    <ns0:GS 
        xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">
        <GS01>SH</GS01>
        <GS02>123456789</GS02>
        <GS03>9987654321</GS03>
        <GS04>20180102</GS04>
        <GS05>2108</GS05>
        <GS06>351</GS06>
        <GS07>X</GS07>
        <GS08>004010</GS08>
    </ns0:GS>
    <TransactionSet DocType="http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_856">
        <ns0:X12_00401_856 
            xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
            <ST>
                <ST01>856</ST01>
                <ST02>000351</ST02>
            </ST>
            <ns0:BSN>
                <BSN01>00</BSN01>
                <BSN02>SID87447</BSN02>
                <BSN03>20180102</BSN03>
                <BSN04>2108</BSN04>
            </ns0:BSN>
            <ns0:DTM>
                <DTM01>011</DTM01>
                <DTM02>20180102</DTM02>
                <DTM03>2109</DTM03>
                <DTM04>ET</DTM04>
            </ns0:DTM>
            <ns0:HLLoop1>
                <ns0:HL>
                    <HL01>1</HL01>
                    <HL03>S</HL03>
                    <HL04>1</HL04>
                </ns0:HL>
                <ns0:MEA>
                    <MEA01>PD</MEA01>
                    <MEA02>G</MEA02>
                    <MEA03>9985</MEA03>
                    <ns0:C001_2>
                        <C00101>LB</C00101>
                    </ns0:C001_2>
                </ns0:MEA>
                <ns0:MEA>
                    <MEA01>PD</MEA01>
                    <MEA02>N</MEA02>
                    <MEA03>9174</MEA03>
                    <ns0:C001_2>
                        <C00101>LB</C00101>
                    </ns0:C001_2>
                </ns0:MEA>
                <ns0:TD1>
                    <TD101>CTN90</TD101>
                    <TD102>55</TD102>
                </ns0:TD1>
                <ns0:TD5>
                    <TD501>B</TD501>
                    <TD502>2</TD502>
                    <TD503>LODQ</TD503>
                    <TD504>M</TD504>
                </ns0:TD5>
                <ns0:TD3>
                    <TD301>TL</TD301>
                    <TD303>1</TD303>
                </ns0:TD3>
                <ns0:REF>
                    <REF01>BM</REF01>
                    <REF02>SID87447</REF02>
                </ns0:REF>
                <ns0:N1Loop1>
                    <ns0:N1>
                        <N101>SU</N101>
                        <N103>92</N103>
                        <N104>8888</N104>
                    </ns0:N1>
                </ns0:N1Loop1>
            </ns0:HLLoop1>
            <ns0:HLLoop1>
                <ns0:HL>
                    <HL01>2</HL01>
                    <HL02>1</HL02>
                    <HL03>O</HL03>
                    <HL04>1</HL04>
                </ns0:HL>
                <ns0:PRF>
                    <PRF01>2018002A</PRF01>
                </ns0:PRF>
                <ns0:REF>
                    <REF01>MH</REF01>
                    <REF02>50945</REF02>
                </ns0:REF>
                <ns0:N1Loop1>
                    <ns0:N1>
                        <N101>SU</N101>
                        <N103>92</N103>
                        <N104>8888</N104>
                    </ns0:N1>
                </ns0:N1Loop1>
            </ns0:HLLoop1>
            <ns0:HLLoop1>
                <ns0:HL>
                    <HL01>3</HL01>
                    <HL02>2</HL02>
                    <HL03>I</HL03>
                    <HL04>0</HL04>
                </ns0:HL>
                <ns0:LIN>
                    <LIN02>BP</LIN02>
                    <LIN03>58706-02010-00V</LIN03>
                    <LIN04>RC</LIN04>
                    <LIN05>S0467</LIN05>
                </ns0:LIN>
                <ns0:SN1>
                    <SN102>1029</SN102>
                    <SN103>EA</SN103>
                </ns0:SN1>
            </ns0:HLLoop1>
            <ns0:HLLoop1>
                <ns0:HL>
                    <HL01>4</HL01>
                    <HL02>2</HL02>
                    <HL03>I</HL03>
                    <HL04>0</HL04>
                </ns0:HL>
                <ns0:LIN>
                    <LIN02>BP</LIN02>
                    <LIN03>61327-0R030-K0V</LIN03>
                    <LIN04>RC</LIN04>
                    <LIN05>B0058</LIN05>
                </ns0:LIN>
                <ns0:SN1>
                    <SN102>858</SN102>
                    <SN103>EA</SN103>
                </ns0:SN1>
            </ns0:HLLoop1>
            <ns0:HLLoop1>
                <ns0:HL>
                    <HL01>5</HL01>
                    <HL02>2</HL02>
                    <HL03>I</HL03>
                    <HL04>0</HL04>
                </ns0:HL>
                <ns0:LIN>
                    <LIN02>BP</LIN02>
                    <LIN03>61328-0R030-K0V</LIN03>
                    <LIN04>RC</LIN04>
                    <LIN05>B0059</LIN05>
                </ns0:LIN>
                <ns0:SN1>
                    <SN102>858</SN102>
                    <SN103>EA</SN103>
                </ns0:SN1>
            </ns0:HLLoop1>
            <ns0:CTT>
                <CTT01>5</CTT01>
            </ns0:CTT>
            <SE>
                <SE01>26</SE01>
                <SE02>000351</SE02>
            </SE>
        </ns0:X12_00401_856>
    </TransactionSet>
    <ns0:GE 
        xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">
        <GE01>1</GE01>
        <GE02>351</GE02>
    </ns0:GE>
</FunctionalGroup>
<ns0:IEA 
    xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">
    <IEA01>1</IEA01>
    <IEA02>000000351</IEA02>
</ns0:IEA>

我试图编写一个XSLT来转换它,此时我有:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006/InterchangeXML"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">
<EDI_Message>
    <Mailbox>
      <Mailbox_Code>
        <xsl:value-of select="normalize-space(/ins0:X12InterchangeXml/ns0:ISA/ISA06/text())" />
      </Mailbox_Code>
      <Mailbox_Qualifier>
        <xsl:value-of select="/ins0:X12InterchangeXml/ns0:ISA/ISA05/text()" />
      </Mailbox_Qualifier>
      <Destination_Qualifier>
        <xsl:value-of select="/ins0:X12InterchangeXml/ns0:ISA/ISA07/text()" />
      </Destination_Qualifier>
      <Destination_Mailbox>
        <xsl:value-of select="normalize-space(/ins0:X12InterchangeXml/ns0:ISA/ISA08/text())"/>
      </Destination_Mailbox>
      <Destination_Code>
      <xsl:value-of select="/ins0:X12InterchangeXml/FunctionalGroup/ns0:GS/GS03/text()" />
        </Destination_Code>
      <Origin_Code>
        <xsl:value-of select="/ins0:X12InterchangeXml/FunctionalGroup/TransactionSet/ns0:X12_00401_856/ns0:HLLoop1/ns0:N1Loop1/ns0:N1/N104/text()" />
        </Origin_Code>
        <Vendor_Code>
          <xsl:value-of select="/ins0:X12InterchangeXml/FunctionalGroup/ns0:GS/GS02/text()" />
          </Vendor_Code>
      <Supplier_Code />
      <Actual_Processor_Duns />
      <Transmission_Count>
        <xsl:value-of select="normalize-space(/ins0:X12InterchangeXml/ns0:ISA/ISA13/text())"/>
      </Transmission_Count>
      <Ship_From_Code />
      <Mailbox_Status>
        <xsl:value-of select="normalize-space(/ins0:X12InterchangeXml/ns0:ISA/ISA15/text())"/>
      </Mailbox_Status>
      <Seller_Identification_Number />
      <Element_Separator>*</Element_Separator>
      <Subelement_Separator>
        <xsl:value-of select="normalize-space(/ins0:X12InterchangeXml/ns0:ISA/ISA16/text())"/>
      </Subelement_Separator>
      <Segment_Terminator_ASCII_Code>126</Segment_Terminator_ASCII_Code>
      <!--Character separating segments, I guess in this case it is the LF character-->
    </Mailbox>
  </EDI_Message>
</xsl:template>

由于Azure创建源文档。我无法真正改变它,或者它是如何使用命名空间声明的。我的问题是在这一点上:

          <Origin_Code>
        <xsl:value-of select="/ins0:X12InterchangeXml/FunctionalGroup/TransactionSet/ns0:X12_00401_856/ns0:HLLoop1/ns0:N1Loop1/ns0:N1/N104/text()" />
        </Origin_Code>

我没有得到价值。我的XPATH是错误的,或者我正在与使用ns0两次的Azure文档发生冲突,一次是

    <ns0:ISA 
    xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema">

然后在同一份文件中:

            <ns0:X12_00401_856 
            xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">

我不知道如何在伪XPATH中指示XPATH是什么

/ins0:X12InterchangeXml[@xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006/InterchangeXML"]/FunctionalGroup[@DocType="http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_856"]/TransactionSet[@DocType="http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_856"]/ns0:X12_00401_856[@xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"]/ns0:HLLoop1[1]/ns0:N1Loop1/ns0:N1/N104/text()

同时还有XSLT支持基本上的XPATH:

/ins0:X12InterchangeXml[@xmlns:ins0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006/InterchangeXML"]/ns0:ISA[@xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema"]/ISA01/text()

2 个答案:

答案 0 :(得分:2)

在样式表中,您可以使用您喜欢和需要的任何前缀,您不需要使用与输入XML相同的前缀,您只需要确保将前缀绑定到与您要选择输入元素。因此,如果您在输入中使用了具有不同命名空间URI的相同前缀,那么在您的XSLT中,使用绑定到第二个URI的不同前缀,并在XPath中使用该第二个前缀,例如, xmlns:ns0="http://schemas.microsoft.com/Edi/X12ServiceSchema" xmlns:ns2="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"作为XSLT中的名称空间声明,然后在XPath表达式中使用这些前缀。

答案 1 :(得分:1)

您可以在XPath表达式中使用您喜欢的任何前缀,它不必与源文档中的前缀相同,只要它绑定到正确的命名空间即可。因此,源文档使用绑定到不同位置的不同命名空间的相同前缀应该不是问题。