Nesting xsl templates and referring multiple templates to the same node?

旧时模样 提交于 2021-02-05 12:10:11

问题


This is my first post :)

I'll try to be clear and nice, thank you in advance for any help! And I apologize that this question is convoluted. :| Any friendly hints on how to better ask the question will be appreciated too!

This is a reduced version of the xml source file I have:

    <?xml version="1.0" encoding="UTF-8"?>
    <FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">
        <ERRORCODE>0</ERRORCODE>
        <DATABASE>FTP XML.fmp12</DATABASE>
        <LAYOUT>
        </LAYOUT>
        <ROW MODID="0" RECORDID="1151">
            <EDI_DC40.TABNAM><DATA>EDI_DC40</DATA></EDI_DC40.TABNAM>
            <E1EDL20.VBELN><DATA>some value</DATA></E1EDL20.VBELN>
            <E1EDL18.QUALF><DATA>data</DATA></E1EDL18.QUALF>
            <E1EDT13.1.QUALF><DATA>123<DATA></E1EDT13.1.QUALF>
            <E1EDT13.2.QUALF><DATA></DATA></E1EDT13.2.QUALF>
            <E1EDL24.POSNR>000001</E1EDL24.POSNR>
            <E1EDL41.QUALI>001</E1EDL41.QUALI>
            <E1EDL37.EXIDV><DATA></DATA></E1EDL37.EXIDV>
            <E1EDL44.VELIN></E1EDL44.VELIN>
            <E1EDL44.POSNR></E1EDL44.POSNR>
        </ROW>
        <ROW MODID="0" RECORDID="1176">
            <EDI_DC40.TABNAM><DATA>EDI_DC40</DATA></EDI_DC40.TABNAM>
            <E1EDL20.VBELN><DATA>vvvv</DATA></E1EDL20.VBELN>
            <E1EDL18.QUALF><DATA>20</DATA></E1EDL18.QUALF>
            <E1EDT13.1.QUALF><DATA>20</DATA></E1EDT13.1.QUALF>
            <E1EDT13.2.QUALF><DATA>30</DATA></E1EDT13.2.QUALF>
            <E1EDL24.POSNR>000001</E1EDL24.POSNR>
            <E1EDL41.QUALI>001</E1EDL41.QUALI>
            <E1EDL37.EXIDV><DATA>eee</DATA></E1EDL37.EXIDV>
            <E1EDL44.VELIN>
                <DATA>1</DATA>
                <DATA>1</DATA>
                <DATA>1</DATA>
            </E1EDL44.VELIN>
            <E1EDL44.POSNR>
                <DATA>000001</DATA>
                <DATA>000002</DATA>
                <DATA>000003</DATA>
            </E1EDL44.POSNR>
        </ROW>
        <ROW MODID="0" RECORDID="1177">
            <EDI_DC40.TABNAM><DATA>EDI_DC40</DATA></EDI_DC40.TABNAM>
            <EDI_DC40.MANDT><DATA>100</DATA></EDI_DC40.MANDT>
            <E1EDL20.VBELN><DATA>vvvv</DATA></E1EDL20.VBELN>
            <E1EDL18.QUALF><DATA>20</DATA></E1EDL18.QUALF>
            <E1EDT13.1.QUALF><DATA>20</DATA></E1EDT13.1.QUALF>
            <E1EDT13.2.QUALF><DATA>30</DATA></E1EDT13.2.QUALF>
            <E1EDL24.POSNR>000002</E1EDL24.POSNR>
            <E1EDL41.QUALI>001</E1EDL41.QUALI>
            <E1EDL37.EXIDV><DATA>eee</DATA></E1EDL37.EXIDV>
            <E1EDL44.VELIN>
                <DATA>1</DATA>
                <DATA>1</DATA>
                <DATA>1</DATA>
            </E1EDL44.VELIN>
            <E1EDL44.POSNR>
                <DATA>000001</DATA>
                <DATA>000002</DATA>
                <DATA>000003</DATA>
            </E1EDL44.POSNR>
        </ROW>
    </FMPDSORESULT>

This is the needed result xml structure (sorry the data is not the same, but the tag names mostly resemble):

    <?xml version="1.0" encoding="utf-8"?>
    <DELVRY05>
        <IDOC BEGIN="1">
            <EDI_DC40 SEGMENT="1">
                <TABNAM>EDI-DC40</TABNAM>
                ...
            </EDI_DC40>
            <E1EDL20 SEGMENT="1">
                <VBELN>649758</VBELN>
                ...
                <E1EDL18 SEGMENT="1">
                    <QUALF>ORI</QUALF>
                </E1EDL18>
                <E1EDT13 SEGMENT="1">
                    <QUALF>007</QUALF>
                    ...
                </E1EDT13>
                <E1EDT13 SEGMENT="1">
                    <QUALF>015</QUALF>
                    ...
                </E1EDT13>
                <E1EDL24 SEGMENT="1">
                    <POSNR>000001</POSNR>
                    ...
                    <E1EDL41 SEGMENT="1">
                        <QUALI>001</QUALI>
                        ...
                    </E1EDL41>
                </E1EDL24>
                <E1EDL24 SEGMENT="1">
                    <POSNR>2</POSNR>
                    ...
                    <E1EDL41 SEGMENT="1">
                        <QUALI>001</QUALI>
                        ...
                    </E1EDL41>
                </E1EDL24>
                <E1EDL24 SEGMENT="1">
                    <POSNR>000003</POSNR>
                    ...
                    <GEWWI>KGM</GEWWI>
                    <E1EDL41 SEGMENT="1">
                        <QUALI>001</QUALI>
                        ...
                    </E1EDL41>
                </E1EDL24>
                <E1EDL24 SEGMENT="1">
                    <POSNR>000004</POSNR>
                    ...
                    <E1EDL41 SEGMENT="1">
                        <QUALI>001</QUALI>
                        <BSTNR>4506192685</BSTNR>
                        <POSEX>00040</POSEX>
                    </E1EDL41>
                </E1EDL24>
                <E1EDL37 SEGMENT="1">
                    <EXIDV>5650327422</EXIDV>
                    ...
                    <EXIDA>H</EXIDA>
                    <E1EDL44 SEGMENT="1">
                        <VELIN>1</VELIN>
                        <POSNR>000001</POSNR>
                        ...
                    </E1EDL44>
                    <E1EDL44 SEGMENT="1">
                        <VELIN>1</VELIN>
                        <POSNR>000002</POSNR>
                        ...
                    </E1EDL44>
                    <E1EDL44 SEGMENT="1">
                        <VELIN>1</VELIN>
                        <POSNR>000003</POSNR>
                        ...
                    </E1EDL44>
                    <E1EDL44 SEGMENT="1">
                        <VELIN>1</VELIN>
                        <POSNR>000004</POSNR>
                        ...
                    </E1EDL44>
                </E1EDL37>
            </E1EDL20>
        </IDOC>
    </DELVRY05>

And this is the gist of the faulty xsl, paining me:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:fm="http://www.filemaker.com/fmpdsoresult" exclude-result-prefixes="fm">
      <xsl:output method="xml" indent="yes"/>

      <xsl:variable name="rename">
            <item from="EDI_DC40.TABNAM" to="TABNAM" />
            <item from="E1EDL20.VBELN" to="VBELN" />
            <item from="E1EDL18.QUALF" to="QUALF" />
            <item from="E1EDT13.1.QUALF" to="QUALF" />
            <item from="E1EDT13.2.QUALF" to="QUALF" />
            <item from="E1EDL24.POSNR" to="POSNR" />
            <item from="E1EDL41.QUALF" to="QUALF" />
            <item from="E1EDL37.EXIDV" to="EXIDV" />
            <item from="E1EDL44.VELIN" to="VELIN" />
      </xsl:variable>

      <xsl:template match="/*">
            <DELVRY05>
              <IDOC BEGIN="1">
                      <xsl:apply-templates select="fm:ROW"/>
              </IDOC>
            </DELVRY05>
      </xsl:template>
      <xsl:template name="headers" match="fm:ROW[1]">
            <EDI_DC40 SEGMENT="1">
              <xsl:apply-templates select="fm:EDI_DC40.TABNAM" mode="rename" />
            </EDI_DC40>
            <E1EDL20 SEGMENT="1">
              <xsl:apply-templates select="fm:E1EDL20.VBELN" mode="rename" />
              <E1EDL18 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDL18.QUALF" mode="rename" />
              </E1EDL18>
              <E1EDT13 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDT13.1.QUALF" mode="rename" />
              </E1EDT13>
              <E1EDT13 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDT13.2.QUALF" mode="rename" />
              </E1EDT13>
              <E1EDL24 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDL24.POSNR" mode="rename" />
                    <E1EDL41 SEGMENT="1">
                      <xsl:apply-templates select="fm:E1EDL41.QUALF" mode="rename" />
                    </E1EDL41>
                    <!-- <xsl:apply-templates select="fm:ROW"/> -->
                    <xsl:apply-templates select="lines"/>
              </E1EDL37>
              <E1EDL37 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDL37.EXIDV" mode="rename" />
                      <E1EDL44 SEGMENT="1">
                            <!-- This I don't have the slightest clue how to solve, since the structure in the XML is similar to an array -->
                            <xsl:apply-templates select="fm:E1EDL44.QUALF[1] | fm:E1EDL44.POSNR[1]" mode="rename" />
                      </E1EDL44>
              </E1EDL37>
            </E1EDL20>
      </xsl:template>

      <xsl:template name="lines" match="fm:ROW">
              <E1EDL41 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDL41.QUALF | fm:E1EDL41.MATNR" mode="rename" />
              </E1EDL41>
      </xsl:template>

      <xsl:template name="packaging" match="fm:ROW">
              <E1EDL44 SEGMENT="1">
                    <xsl:apply-templates select="fm:E1EDL44.QUALF | fm:E1EDL44.POSNR" mode="rename" />
              </E1EDL44>
      </xsl:template>



      <xsl:template match="*" mode="rename">
            <xsl:element name="{document('')//xsl:variable[@name = 'rename']/item[@from = local-name(current())]/@to}">
              <xsl:value-of select="."/>
            </xsl:element>
      </xsl:template>
    </xsl:stylesheet>

As you can see, I have several issues:

  • I think I need to somehow nest the templates, but that doesn't give the desired result (the nested template wrongly shows up after the tag E1EDL20 has been closed). How can I do this properly?

  • What template match can I use for the 'array' data in the E1EDL44 tag?

  • That match problem with unique identifiers is, as clearly seen in the xsl a problem, because I have to nest this structure (I know I shouldn't do it, but I don't see how else to achieve this structure):

     <DELVRY05>
         <IDOC BEGIN="1">
             <EDI_DC40 SEGMENT="1">
                 <!-- header data unique tags - template on first ROW -->
             </EDI_DC40>
             <E1EDL20 SEGMENT="1">
                 <!-- more header data unique tags, this has to be done seperate, because of the container tag, I assume 2 nd template on the first ROW -->
                 <E1EDL24 SEGMENT="1">
                     <!-- line data tags; random n, corresponds to the ROWS in the source file -->
                 </E1EDL24>
                 <E1EDL37 SEGMENT="1">
                     <!-- again header data unique tags -->
                     <E1EDL44 SEGMENT="1">
                         <!-- 'arrayed' data, random n lines, I don't even know how to do this one! It should also only appear one time, only for the first line ( so I thought this would need a third template on the first ROW  -->
                     </E1EDL44>
                 </E1EDL37>
             </E1EDL20>
         </IDOC>
     </DELVRY05>
    

How do I get the E1EDL44 nested in there?

Please help me! Thank you very much for every tidbit of help you can offer or steer me in the right direction. Tom

来源:https://stackoverflow.com/questions/64966059/nesting-xsl-templates-and-referring-multiple-templates-to-the-same-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!