请参考问题“以XML转义反斜杠并使用XSLT拆分为单独的xml节点”

时间:2018-10-10 12:59:21

标签: xml xslt xslt-1.0

我需要将以下XML转换为所需的输出-

<?xml version="1.0" encoding="UTF-8"?>
    <properties>
    <entry>
        <key>first_node/P_NODE</key>
        <value>
            <genericData>
                <identifier>first_node/P_NODE</identifier>
                <properties>
                    <entry>
                        <key>second_node</key>
                        <value>2</value>
                    </entry>
                    <entry>
                        <key>third_node/fourth_node/fifth_node</key>
                        <value>345</value>
                    </entry>
                    <entry>
                        <key>sixth_node/seventh_node</key>
                        <value>67</value>
                    </entry>
                    <entry>
                        <key>eigth_node</key>
                        <value>8</value>
                    </entry>
                    <entry>
                        <key>ninth_node</key>
                        <value>
                            <genericData>
                                <identifier>ninth_node</identifier>
                                <properties>
                                    <entry>
                                        <key>tenth_node</key>
                                        <value>10</value>
                                    </entry>
                                    <entry>
                                        <key>eleventh_node/twelveth_node</key>
                                        <value>1112</value>
                                    </entry>
                                </properties>
                            </genericData>


                            <genericData>
                                <identifier>ninth_node</identifier>
                                <properties>
                                    <entry>
                                        <key>x_node</key>
                                        <value>10</value>
                                    </entry>
                                    <entry>
                                        <key>y_node</key>
                                        <value>1112</value>
                                    </entry>
                                </properties>
                            </genericData>


                        </value>
                    </entry>
                </properties>
            </genericData>
        </value>
    </entry>
    <entry>
        <key>tirteenth_node</key>
        <value>
            <genericData>
                <identifier>tirteenth_node</identifier>
                <properties>
                    <entry>
                        <key>fourteenth_node</key>
                        <value>14</value>
                    </entry>
                    <entry>
                        <key>fifteenth_node/sixteenth_node</key>
                        <value>1516</value>
                    </entry>
                </properties>
            </genericData>
        </value>
    </entry>
    <entry>
        <key>seventeeth_node/eighteenth_node</key>
        <value>1718</value>
    </entry>
    <entry>
        <key>nineteenth_node/twenth_node</key>
        <value>1920</value>
    </entry>
    <entry>
        <key>twentyfirst_node</key>
        <value>21</value>
    </entry>
</properties>

所需的输出:

 <?xml version="1.0" encoding="UTF-8"?>
    <properties>
        <first_node>
          <P_NODE>
             <second_node>2</second_node>
             <third_node>
                <fourth_node>
                   <fifth_node>345</fifth_node>
                </fourth_node>
             </third_node>
             <sixth_node>
                <seventh_node>67</seventh_node>
             </sixth_node>
             <eigth_node>8</eigth_node>
             <ninth_node>
                <tenth_node>10</tenth_node>
                <eleventh_node>
                   <twelveth_node>1112</twelveth_node>
                </eleventh_node>
             </ninth_node>
             <ninth_node>
                <x_node>10</x_node>
                <y_node>1112</y_node>
             </ninth_node>
          </P_NODE>
       </first_node>
        <tirteenth_node>
          <fourteenth_node>14</fourteenth_node>
          <fifteenth_node>
             <sixteenth_node>1516</sixteenth_node>
          </fifteenth_node>
       </tirteenth_node>
        <seventeeth_node>
          <eighteenth_node>1718</eighteenth_node>
       </seventeeth_node>
        <nineteenth_node>
          <twenth_node>1920</twenth_node>
       </nineteenth_node>
        <twentyfirst_node>21</twentyfirst_node>
    </properties>

请让我知道可以用于实现上述目的的XSLT。

我使用了通过问题“ Escape backslash in XML and split as separate xml node using XSLT”回答​​的代码。但是,当我对上述XML使用相同的代码时,它将在父级内部打印子级元素,但也将其打印为独立元素。注意:已对问题进行了修改,以代表实际的问题陈述。

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要添加第二个模板,匹配下面的entry个节点,genericData,因为您需要为每个这样的节点重复代码

<xsl:template match="entry[value/genericData]">
  <xsl:for-each select="value/genericData">
    <xsl:call-template name="splitter">
      <xsl:with-param name="path" select="identifier" />
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

您还需要调整检查后代entry节点的代码,以使其仅获得entry节点的第一级,而不是所有后代

 <xsl:when test="self::genericData">
   <xsl:apply-templates select="properties/entry" />
 </xsl:when>

尝试使用此XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="entry">
  <xsl:call-template name="splitter">
    <xsl:with-param name="path" select="key" />
    <xsl:with-param name="value" select="value" />
  </xsl:call-template>
</xsl:template>

<xsl:template match="entry[value/genericData]">
  <xsl:for-each select="value/genericData">
    <xsl:call-template name="splitter">
      <xsl:with-param name="path" select="identifier" />
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="splitter">
  <xsl:param name="path" />
  <xsl:param name="value" />
  <xsl:choose>
    <xsl:when test="contains($path, '/')">
      <xsl:element name="{substring-before($path, '/')}">
        <xsl:call-template name="splitter">
          <xsl:with-param name="path" select="substring-after($path, '/')" />
          <xsl:with-param name="value" select="$value" />
        </xsl:call-template>
      </xsl:element>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{$path}">
        <xsl:choose>
          <xsl:when test="self::genericData">
            <xsl:apply-templates select="properties/entry" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$value" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>