输出树生成的XSLT递归

时间:2016-11-01 20:06:09

标签: xslt

我有xml以下,我需要使用xslt

转换为以下xml

在输入xml中,我引用了最顶层节点,其子节点如下:

<?xml version="1.0"?>
<referenced>
  <name>PocTree/PocTree.services:getReferencesForServices</name>
  <LOCK_STATUS>3</LOCK_STATUS>
  <type>
    <svc_type>flow</svc_type>
    <svc_subtype>default</svc_subtype>
  </type>
  <path/>
  <isPub>false</isPub>
  <isNotification>false</isNotification>
  <isFlowServiceDisabled>false</isFlowServiceDisabled>
  <status>reference</status>
  <reference>
    <name>WmRoot/wm.server.ns.dependency:getReferenced</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;0</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>WmPublic/pub.xml:documentToXMLString</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;1</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>PocTree/PocTree.services:jkl</name>
    <LOCK_STATUS>3</LOCK_STATUS>
    <type>
      <svc_type>xsltservice</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;2</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>WmPublic/pub.xml:xmlStringToXMLNode</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;3</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>WmPublic/pub.xml:xmlNodeToDocument</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;4</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>PocTree/PocTree.services:ghi</name>
    <LOCK_STATUS>4</LOCK_STATUS>
    <type>
      <svc_type>xsltservice</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;5</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <reference>
    <name>WxPackageTree/WxPackageTree.doc:NodeInfo</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <type_name>record</type_name>
    </type>
    <path>/Service Path;1.0/signature;2;subPath;"/nodes;4;0;WxPackageTree.doc:NodeInfo"</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>reference</status>
    <reference>
      <name>WxPackageTree/WxPackageTree.doc:NodeInfo</name>
      <LOCK_STATUS>2</LOCK_STATUS>
      <type>
        <type_name>record</type_name>
      </type>
      <path>/children;4;1;WxPackageTree.doc:NodeInfo</path>
      <isPub>false</isPub>
      <isNotification>false</isNotification>
      <isFlowServiceDisabled>false</isFlowServiceDisabled>
      <status>recursive</status>
    </reference>
    <reference>
    <name>PocTree/PocTree.docs:nodeInfo</name>
    <LOCK_STATUS>4</LOCK_STATUS>
    <type>
      <type_name>record</type_name>
    </type>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;1/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;2/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;3/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>reference</status>
    <reference>
      <name>PocTree/PocTree.docs:nodeInfo</name>
      <LOCK_STATUS>4</LOCK_STATUS>
      <type>
        <type_name>record</type_name>
      </type>
      <path>/children;4;1;PocTree.docs:nodeInfo</path>
      <isPub>false</isPub>
      <isNotification>false</isNotification>
      <isFlowServiceDisabled>false</isFlowServiceDisabled>
      <status>recursive</status>
    </reference>
  </reference>
  </reference>
</referenced> 

预期输出XML结构应该类似于:

<?xml version="1.0"?>
<nodeInfo>
  <name>PocTree/PocTree.services:getReferencesForServices</name>
  <LOCK_STATUS>3</LOCK_STATUS>
  <type>
    <svc_type>flow</svc_type>
    <svc_subtype>default</svc_subtype>
  </type>
  <path/>
  <isPub>false</isPub>
  <isNotification>false</isNotification>
  <isFlowServiceDisabled>false</isFlowServiceDisabled>
  <status>reference</status>
  <children>
    <name>WmRoot/wm.server.ns.dependency:getReferenced</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;0</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </children>
  <children>
    <name>WmPublic/pub.xml:documentToXMLString</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;1</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </reference>
  <children>
    <name>PocTree/PocTree.services:jkl</name>
    <LOCK_STATUS>3</LOCK_STATUS>
    <type>
      <svc_type>xsltservice</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;2</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </children>
  <children>
    <name>WmPublic/pub.xml:xmlStringToXMLNode</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;3</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </children>
  <children>
    <name>WmPublic/pub.xml:xmlNodeToDocument</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <svc_type>java</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;4</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </children>
  <children>
    <name>PocTree/PocTree.services:ghi</name>
    <LOCK_STATUS>4</LOCK_STATUS>
    <type>
      <svc_type>xsltservice</svc_type>
      <svc_subtype>unknown</svc_subtype>
    </type>
    <path>/Flow Path;1.0/INVOKE;5</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>resolved</status>
  </children>
  <children>
    <name>WxPackageTree/WxPackageTree.doc:NodeInfo</name>
    <LOCK_STATUS>2</LOCK_STATUS>
    <type>
      <type_name>record</type_name>
    </type>
    <path>/Service Path;1.0/signature;2;subPath;"/nodes;4;0;WxPackageTree.doc:NodeInfo"</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>reference</status>
    <children>
      <name>WxPackageTree/WxPackageTree.doc:NodeInfo</name>
      <LOCK_STATUS>2</LOCK_STATUS>
      <type>
        <type_name>record</type_name>
      </type>
      <path>/children;4;1;WxPackageTree.doc:NodeInfo</path>
      <isPub>false</isPub>
      <isNotification>false</isNotification>
      <isFlowServiceDisabled>false</isFlowServiceDisabled>
      <status>recursive</status>
    </children>
    <children>
    <name>PocTree/PocTree.docs:nodeInfo</name>
    <LOCK_STATUS>4</LOCK_STATUS>
    <type>
      <type_name>record</type_name>
    </type>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;1/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;2/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <path>/Flow Path;1.0/MAP;6/MAPCOPY;3/to_field;-2;subPath;"/nodeInfo;4;0;PocTree.docs:nodeInfo"</path>
    <isPub>false</isPub>
    <isNotification>false</isNotification>
    <isFlowServiceDisabled>false</isFlowServiceDisabled>
    <status>reference</status>
    <children>
      <name>PocTree/PocTree.docs:nodeInfo</name>
      <LOCK_STATUS>4</LOCK_STATUS>
      <type>
        <type_name>record</type_name>
      </type>
      <path>/children;4;1;PocTree.docs:nodeInfo</path>
      <isPub>false</isPub>
      <isNotification>false</isNotification>
      <isFlowServiceDisabled>false</isFlowServiceDisabled>
      <status>recursive</status>
    </children>
  </children>
  </children>
</nodeInfo>

但是获得预期输出xml的标准是来自输入xml的一个子节点的值,即本节

<type>
    <svc_type>flow</svc_type>
    <svc_subtype>default</svc_subtype>
  </type>

svc_type标签应该具有值&#34; flow&#34;或&#34; javaservice&#34;然后才映射到输出xml结构。

如何微调下面的xslt,它从输入xml到输出xml进行简单复制(简化所有已完成的操作是重命名为nodeInfo,并引用子节点。)

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

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

    <xsl:template match="referenced">
        <nodeInfo>
            <xsl:apply-templates />
        </nodeInfo>
    </xsl:template>

    <xsl:template match="reference//type/svc_type[contains(., 'flow' or 'java')]">
        <children>
            <xsl:apply-templates />
        </children>
    </xsl:template>
</xsl:stylesheet>

感谢您对此主题的帮助。

此致 Nischal

1 个答案:

答案 0 :(得分:0)

如果您想根据条件转换reference,请将条件放入谓词中,因此将match="reference//type/svc_type[contains(., 'flow' or 'java')]更改为match="reference[type/svc_type[contains(., 'flow') or contains(., 'java')]]"