具有多个匹配条件的标识模板的XSLT和删除未使用的节点

时间:2016-11-02 17:41:46

标签: 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>flow</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>java</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>java</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>xsltservice</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>java</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>java</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>xsltservice</type>
        <path>/Flow Path;1.0/INVOKE;5</path>
        <isPub>false</isPub>
        <isNotification>false</isNotification>
        <isFlowServiceDisabled>false</isFlowServiceDisabled>
        <status>resolved</status>
      </children>
    </nodeInfo>

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

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

哪个时候有一个额外的标签,这个意思可以有子节点或只有。

 <type>
    <type_name>record</type_name>
  </type>

现在应该设计预期的输出XML,只选择输入xml中的那些<refrence>标签,其中<svc_type>具有'flow'或'javaservice'或'xsltservice'的值,并省略{ {1}} <refrence>属于<type_name>父节点且具有任何值的标记。

此外,<type>的内容应该映射到<svc_type>的值,输出xml不应包含其父节点<type>的子节点<svc_type><svc_subtype>

请帮助解决这个问题。

包括为实现输入XML的过滤而编写的xslt

<type>

但是上面的xslt仍未涵盖此部分 -   <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 contains(., 'java') or contains(., 'xsltservice')]]) and (reference[type/type_name[not(contains(.,'record'))]])"> <children> <xsl:apply-templates /> </children> </xsl:template> </xsl:stylesheet> 应映射到<svc_type>的值,输出xml不应包含其父节点<type>的子节点<svc_type><svc_subtype>

1 个答案:

答案 0 :(得分:0)

你需要改变你的逻辑。您不必考虑要复制的节点,而是要考虑您不想复制的内容,并编写模板以匹配这些节点,并忽略它们。

然后,身份模板将处理您要复制的所有内容。

因此,您说要包含输入xml中的<reference>个标记,其中<svc_type>的值为&#39; flow&#39;或者&#39; javaservice&#39;或者&#39; xsltservice&#39;。与此相反的是,您希望排除不会将<reference>设置为这三个值中的任何一个的<svc_type>

<xsl:template match="reference[type/svc_type[not(contains(., 'flow') or contains(., 'java') or contains(., 'xsltservice'))]]" />

你的其他规则已经是一个关于删除的规则,因此可以直接进行而无需任何逻辑反转

<xsl:template match="reference[type/type_name]" priority="2" />

此处可能不需要优先级,除非您的reference标记同时具有type/type_nametype/svc_type,在这种情况下,模板之间会发生冲突< / p>

试试这个XSLT

<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">
        <children>
            <xsl:apply-templates />
        </children>
    </xsl:template>

    <xsl:template match="type[svc_type]">
        <type>
            <xsl:value-of select="svc_type" />
        </type>
    </xsl:template>

    <xsl:template match="reference[type/svc_type[not(contains(., 'flow') or contains(., 'java') or contains(., 'xsltservice'))]]" />

    <xsl:template match="reference[type/type_name]" priority="2" />
</xsl:stylesheet>

另请注意添加模板以便将svc_type上的转换处理为type