1

XML をある構造から別の構造に変換する必要があります。これを行う方法がわかりません。最初の xml は実際にはマスター詳細のようには見えませんが、結果の XML はマスト詳細のように見える必要があります。

   <?xml version = '1.0' encoding = 'UTF-8'?>
    <Root-Element xmlns="http://example.com/ReadProductB">
       <RECORD1>
          <C2>QGGG9.A1-1</C2>
       </RECORD1>
       <RECORD2>
          <C2>xflowcode</C2>
          <C3>FL1</C3>
       </RECORD2>
       <RECORD2>
          <C2>xtilo</C2>
          <C3>1234</C3>
       </RECORD2>
       <RECORD2>
          <C2>xwat</C2>
          <C3>75</C3>
       </RECORD2>
       <RECORD1>
          <C2>QGGG9.A1-2</C2>
       </RECORD1>
       <RECORD2>
          <C2>xflowcode</C2>
          <C3>FL1</C3>
       </RECORD2>
       <RECORD2>
          <C2>xtilo</C2>
          <C3>1234</C3>
       </RECORD2>
       <RECORD2>
          <C2>xwat</C2>
          <C3>75</C3>
       </RECORD2>
    </Root-Element>

これが私が望む結果です。

   <?xml version = '1.0' encoding = 'UTF-8'?>
    <ns0:ProductCollection xmlns:ns0="http://example.com/ReadProductBB" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <ns0:RECORD1>
          <ns0:C2>QGGG9.A1-1</ns0:C2>
          <ns0:RECORD2Collection>
             <ns0:RECORD2>
                <ns0:C2>xflowcode</ns0:C2>
                <ns0:C3>FL1</ns0:C3>
             </ns0:RECORD2>
             <ns0:RECORD2>
                <ns0:C2>xtilo</ns0:C2>
                <ns0:C3>1234</ns0:C3>
             </ns0:RECORD2>
             <ns0:RECORD2>
                <ns0:C2>xwat</ns0:C2>
                <ns0:C3>75</ns0:C3>
             </ns0:RECORD2>
          </ns0:RECORD2Collection>
       </ns0:RECORD1>
       <ns0:RECORD1>
          <ns0:C2>QGGG9.A1-2</ns0:C2>
          <ns0:RECORD2Collection>
             <ns0:RECORD2>
                <ns0:C2>xflowcode</ns0:C2>
                <ns0:C3>FL1</ns0:C3>
             </ns0:RECORD2>
             <ns0:RECORD2>
                <ns0:C2>xtilo</ns0:C2>
                <ns0:C3>1234</ns0:C3>
             </ns0:RECORD2>
             <ns0:RECORD2>
                <ns0:C2>xwat</ns0:C2>
                <ns0:C3>75</ns0:C3>
             </ns0:RECORD2>
          </ns0:RECORD2Collection>
       </ns0:RECORD1>
    </ns0:ProductCollection>

助けてください。ありがとう

4

2 に答える 2

0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:b="http://example.com/ReadProductB"
    xmlns:bb="http://example.com/ReadProductBB"
    version="1.0">
    <xsl:output indent="yes"/>

    <!--
        identity template
        https://en.wikipedia.org/wiki/Identity_transform
    -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--
        converts elements from the namespace "http://example.com/ReadProductB"
        to "http://example.com/ReadProductBB" 
    -->
    <xsl:template match="b:*">
        <xsl:element name="bb:{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="b:Root-Element">  
        <!--Convert b:Root-Element into bb:ProductCollection-->
        <bb:ProductCollection>
            <!--and then "push" only the b:RECORD1 elements -->
            <xsl:apply-templates select="b:RECORD1"/>
        </bb:ProductCollection>
    </xsl:template>

    <xsl:template match="b:RECORD1">
        <!--Convert the RECORD1 to new namespace -->
        <bb:RECORD1>
            <!-- "push" it's children 
            (which will match the generic templates above)-->
            <xsl:apply-templates select="@*|node()"/>
            <bb:RECORD2Collection>
              <!--and "push" the RECORD2 elements that are 
                between this RECORD1 element and the next(or the end) -->
              <xsl:apply-templates select="following-sibling::b:RECORD2
                      [generate-id(preceding-sibling::b:RECORD1[1])
                       = generate-id(current())]" />
            </bb:RECORD2Collection>
        </bb:RECORD1>
    </xsl:template>

</xsl:stylesheet>
于 2013-05-05T14:38:52.687 に答える