XSLT - 打印子节点名称(树结构/文件夹资源管理器样式)

时间:2016-04-14 02:09:29

标签: xml xslt

需要专家帮助才能使用XSLT获得所需的输出。基本上试图找到一种方法来做树结构或文件夹导航。如果根节点有子节点,我需要设置type : object。并将子节点名称放在properties:下。 xml是动态的,我不知道节点名称,因为它来自数据库。

XML

<response>
    <Node_A_1>
        <Node_B_1>P</Node_B_1>
        <Node_B_2>
            <Node_C_1>B</Node_C_1>
            <Node_C_2>B</Node_C_2>
        </Node_B_2>
        <Node_B_3>4110838</Node_B_3>            
    </Node_A_1>
    <Node_A_2>
        <Node_B_6>Ok</Node_B_6>
        <Node_B_7>
            <Node_C_6>2016-04-13T08:57:23.552-05:00</Node_C_6> 
        </Node_B_7>
        <Node_B_8>200</Node_B_8>
    </Node_A_2> 

预期结果

response:
    type: Object
    properties: 
      Node_A_1:
         type: Object
         properties: 
           Node_B_1:
             type: string 
           Node_B_2:
             type: Object
             properties:
                Node_C_1:
                   type: string
                Node_C_2:
                   type: string
           Node_B_3:
              type: string
      Node_A_2:
         type: object
         properties:
            Node_B_6
               type: String
            Node_B_7
               type: object  
               properties:
                  Node_C_6
                     type: String
            Node_B_8
                type: String

到目前为止,我已经提出了以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
    <xsl:template match="*" name="makeUL">
        <ul>
            <xsl:apply-templates select="*"/>
        </ul>
    </xsl:template>
    <xsl:template match="*">
        <li>
            <xsl:value-of select="name()" /> 
            <xsl:call-template name="makeUL"/> 
        </li>
    </xsl:template>
</xsl:stylesheet>

当前输出

response

    Node_A_1
        Node_B_1
        Node_B_2
            Node_C_1
            Node_C_2
        Node_B_3
    Node_A_2
        Node_B_6
        Node_B_7
            Node_C_6
        Node_B_8

现在我需要找到一种方法来检查节点是否有子节点,这样我就可以打印type:object并打印type:string如果节点没有任何子节点。这些属性(类型,属性)不是XML的一部分,而是基于子节点的存在而派生的。

2 个答案:

答案 0 :(得分:0)

以这种方式试试吗?

XSLT 1.0

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

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

<xsl:template match="*[*]">
    <li>
        <xsl:value-of select="name()"/>
        <br/>
        <xsl:text>type: object</xsl:text>
        <br/>
        <xsl:text>properties:</xsl:text>
        <ul>
            <xsl:apply-templates/>
        </ul>
    </li>
</xsl:template>

<xsl:template match="*">
    <li>
        <xsl:value-of select="name()"/>
        <br/>
        <xsl:text>type: string </xsl:text>
    </li>
</xsl:template>

</xsl:stylesheet>

编辑:

  

很抱歉这个混乱。这不适用于浏览器UI目的。我是   试图发送txt响应

对于文本输出,请尝试:

XSLT 1.0

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

<xsl:template match="*[*]">
    <xsl:param name="indent"/>
    <xsl:value-of select="$indent"/>
    <xsl:value-of select="name()"/>
    <xsl:value-of select="concat('&#10;', $indent)"/>
    <xsl:text>    type: object</xsl:text>
    <xsl:value-of select="concat('&#10;', $indent)"/>
    <xsl:text>    properties:</xsl:text>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates>
        <xsl:with-param name="indent" select="concat($indent, '      ')"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="*">
    <xsl:param name="indent"/>
    <xsl:value-of select="$indent"/>
    <xsl:value-of select="name()"/>
    <xsl:value-of select="concat('&#10;', $indent)"/>
    <xsl:text>   type: string </xsl:text>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

基本的解决方案就像是从michael.hor257k中获得了具有子元素的元素的模板:

<xsl:template match="*[*]">

和没有子元素的一个:

<xsl:template match="*[not(*)]">

谓词([not(*)])并不是因为匹配*[*]具有聘用优先权。

尝试:

    <xsl:template match="*[*]">
        <li>
            <span><xsl:value-of select="name()" /></span>
            <ul>
                <li>type: Object</li>
                <li>
                    <span>
                        <xsl:text>properties:</xsl:text>
                    </span>
                    <ul>
                        <xsl:apply-templates select="*"/>
                    </ul>
                </li>
            </ul>
        </li>
    </xsl:template>

    <xsl:template match="*[not(*)]">
        <li>
            <span><xsl:value-of select="name()" /></span>
            <ul>
                <li>type: string</li>
            </ul>
        </li>
     </xsl:template>

将生成:

  •   响应   
          
    • type:Object
    •     
    •       特性:       
                
      •           Node_A_1           
                      
        • type:Object
        •             
        •               特性:               
                            
          •                   Node_B_1                   
                                  
            • type:string
            •                   
                            
          •                 
          •                   Node_B_2                   
                                  
            • type:Object
            •                     
            •                       特性:                       
                                        
              •                           Node_C_1                           
                                              
                • type:string
                •                           
                                        
              •                         
              •                           Node_C_2                           
                                              
                • type:string
                •                           
                                        
              •                       
                                  
            •                   
                            
          •                 
          •                   Node_B_3                   
                                  
            • type:string
            •                   
                            
          •               
                      
        •           
                
      •         
      •           Node_A_2           
                      
        • type:Object
        •             
        •               特性:               
                            
          •                   Node_B_6                   
                                  
            • type:string
            •                   
                            
          •                 
          •                   Node_B_7                   
                                  
            • type:Object
            •                     
            •                       特性:                       
                                        
              •                           Node_C_6                           
                                              
                • type:string
                •                           
                                        
              •                       
                                  
            •                   
                            
          •                 
          •                   Node_B_8                   
                                  
            • type:string
            •                   
                            
          •               
                      
        •           
                
      •       
          
    •   
  • 相关问题