2016-11-17 138 views
0

我有下面的xml,我需要,我們需要利用我們已經來達到的XSLT遞歸

輸入XML我已經引用爲具有子節點如下最頂層節點XSLT轉換爲預期輸出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> 

XSLT寫:

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

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

<xsl:template match="referenced | reference"> 
    <nodeInfo> 
     <name> 
     <xsl:value-of select="substring-after(name, '/')"/> 
     </name> 
     <rowId> 
      <xsl:value-of select="generate-id()"/> 
     </rowId> 
     <parentId> 
      <xsl:value-of select="generate-id(..)"/> 
     </parentId> 
<xsl:apply-templates select="isFlowServiceDisabled"/> 
      <xsl:apply-templates select="type"/> 
      <xsl:copy-of select="isPub"/> 
      <xsl:copy-of select="isFlowServiceDisabled"/> 
    </nodeInfo> 
    <xsl:apply-templates select="reference"/> 
</xsl:template> 


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

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

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

    <xsl:template match="isFlowServiceDisabled" > 

     <xsl:choose> 
      <xsl:when test="isFlowServiceDisabled[contains(., 'false')]"> 
       <isFlowServiceDisabled> 
        <xsl:value-of select="isFlowServiceDisabled" /> 
       </isFlowServiceDisabled> 
      </xsl:when> 
      <xsl:otherwise/> 

     </xsl:choose> 

    </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[not(contains(., 'ConnectionData') or contains(., 'webServiceDescriptor'))]]" priority="2" /> 

    <xsl:template match="reference[type/trigger_type[not(contains(., 'jms-trigger') or contains(., 'broker-trigger'))]]" priority="2" /> 




</xsl:stylesheet> 

目標XML即時尋找

<nodeInfo> 
    - 
    - 
    <rowId>1</rowId> 
    <parentNodeId>0</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>2</rowId> 
    <parentNodeId>1</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>3</rowId> 
    <parentNodeId>1</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>4</rowId> 
    <parentNodeId>1</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>5</rowId> 
    <parentNodeId>4</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>6</rowId> 
    <parentNodeId>4</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>7</rowId> 
    <parentNodeId>1</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>8</rowId> 
    <parentNodeId>7</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>9</rowId> 
    <parentNodeId>7</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 
<nodeInfo> 
    - 
    - 
    <rowId>10</rowId> 
    <parentNodeId>9</parentNodeId> 
    <isFlowServiceDisabled>false</isFlowServiceDisabled> 
</nodeInfo> 

問題我現在面臨的是如預期的標籤<isFlowServiceDisabled>所寫的條件無法正常工作。含義我得到的標籤爲空值,不知道我的XSLT有什麼問題。除了XSLT中的其他條件外,我還希望強制執行一個規則,如果節點<isFlowServiceDisabled>的值爲「false」,那麼只包含或不包含。

請告訴我什麼是空值。

回答

0

我覺得下面應該是固定的,

你不需要下面的模板。你也不應該使用xsl:在這裏選擇只會做。此外,您還添加了xsl:copy-of select =「isFlowServiceDisabled」,它將覆蓋您的模板調用。我已經刪除它。

<xsl:template match="isFlowServiceDisabled" > 

    <xsl:choose> 
     <xsl:when test="isFlowServiceDisabled[contains(., 'false')]"> 
      <isFlowServiceDisabled> 
       <xsl:value-of select="isFlowServiceDisabled" /> 
      </isFlowServiceDisabled> 
     </xsl:when> 
     <xsl:otherwise/> 

    </xsl:choose> 

</xsl:template> 

應該不用模板來完成,

<xsl:if test="isFlowServiceDisabled = 'false'"> 
    <isFlowServiceDisabled> 
      false 
    </isFlowServiceDisabled> 
</xsl:if> 

編輯:更新XML和XSL適當所需的輸出。 檢查演示在這裏:http://xsltransform.net/ejivdHb/8

<?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>true</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>true</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> 

XSL

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

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

    <xsl:template match="referenced | reference"> 
    <nodeInfo> 
    <name> 
     <xsl:value-of select="substring-after(name, '/')"/> 
    </name> 
    <rowId> 
     <xsl:value-of select="generate-id()"/> 
    </rowId> 
    <parentId> 
     <xsl:value-of select="generate-id(..)"/> 
    </parentId> 

     <xsl:apply-templates select="type"/> 
     <xsl:copy-of select="isPub"/> 

    <xsl:if test="isFlowServiceDisabled = 'false'"> 
     <isFlowServiceDisabled>false</isFlowServiceDisabled> 
    </xsl:if> 
</nodeInfo> 
<xsl:apply-templates select="reference"/> 
</xsl:template> 


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

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

    <xsl:template match="type[trigger_type]"> 
    <type> 
     <xsl:value-of select="trigger_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[not(contains(., 'ConnectionData') or contains(., 'webServiceDescriptor'))]]" priority="2" /> 

    <xsl:template match="reference[type/trigger_type[not(contains(., 'jms-trigger') or contains(., 'broker-trigger'))]]" priority="2" /> 

    </xsl:stylesheet> 
+0

謝謝您的輸入。那麼再次我有一個問題:當是已經在那裏,因爲我想整個我值「真」,那麼在我的XML輸出它與空值來跳過如果是真實的。如何爲此調整xslt? – Nis

+0

@Nis請接受我對你原來問題的回答。然而,對於新的要求,你可以檢查isServiceDisabled類似isFlowServiceDisabled,把參考塊進去 – ScanQR

+0

@Nis沒有這個解決您的查詢? – ScanQR