用于转换的通用XSLT代码 [英] Generic XSLT code for transformation

查看:99
本文介绍了用于转换的通用XSLT代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一些挑战...



是否可以使用适用于任何XML层次结构的XSL代码进行转换它进入嵌套的HTML列表?



示例XML:



 < span class =code-keyword><   Base  >  
< >
< Child / >
< / >
< 孩子 >
< ChildOne / >
< ChildOne / >
< ChildOne >
< ChildTwo / >
< ChildTwo / >
< / ChildOne >
< / Child >
< / Parent >
< Parent >
< 儿童 >
< ChildOne >
< ChildTwo >
& lt; ChildThree >
< ChildFour / >
< / ChildThree >
< ChildThree / >
< / ChildTwo >
< / ChildOne >
< ChildOne / >
< ChildOne / >
< / Child >
< Child / >
< Child / < span class =code-keyword>>
< / Parent >
< / Base >



所需结果:



 <   ul  >  
< li > Parent1
< ul >
< li > 儿童< / li >
< li > 儿童
< ul >
< li > ChildOne < / li < span class =code-keyword>>
< li > ChildOne>
< ul >
< li > ChildTwo < / li >
< li > ChildTwo < / li >
< / ul >
< / li > ;
< / ul >
< / li >
< < span class =code-leadattribute> / ul >
< / li >
< li > Parent2
< ; ul >
< li > 儿童
< ul >
< span class =code-keyword>< li > ChildOne
< ul >
< li > ChildTwo
< ul >
< li > ChildThree
< ul >
< li > ; ChildFour < / li >
< / ul >
< / li > ;
< li > ChildThree < / li > ;
< / ul >
< / li >
< / ul >
< / li >
< li > ChildOne < / li >
< li > ChildOne < / li >
< / ul >
< / li >
< li > < / li >
< / ul >
< / li >
< / ul >



所以逻辑是如果有其他元素(压制el。)在一个元素(上层el。) - 被压制的el。通过< li>名称的上层el获得环绕。< ul>所有被抑制的el。< / ul>< / li>等等。



我的观点是这个XSL代码不是通过元素或属性的值或名称,而是通过条件检查,如果有其他元素属于一个特定的元素。



Fe,如果ChildThree没有ChildFour< / li>在它本身之下,它只是< li> ChildThree>而不是



 <   li  >  ChildThree 
< ul >
< li > ChildFour < / li >
< / ul >
< / li >



希望我自己解释清楚。 :)

解决方案

这让你无需付出太多努力。不确定你是如何/为什么过滤掉你的一些元素,并且你在你想要的输出中使用索引是不一致的,所以把它留下来:

 < span class =code-summarycomment><?  xml     version   =  1.0   编码  =  utf-8   >  
< xsl:stylesheet version = 1.0 X mlns:xsl = http://www.w3.org/1999/XSL/Transform

xmlns:msxsl = urn:schemas-microsoft-com:xslt exclude-result-prefixes = msxsl >
< xsl:output method < span class =code-keyword> = xml 缩进 = / >

< xsl:template 匹配 = * >
< ul >
< xsl:for-each 选择 = * >
< li >
< xsl: value-of 选择 = name(。) / >
< xsl:if test = count(*)> 0 >
< xsl:apply-templates 选择 = > < / xsl:apply-templates >
< / xsl:if >
< / li >
< / xsl:for-each >
< / ul >
< / xsl:模板 >
< / xsl:stylesheet >


以上解决方案正常,但它不会留下通过添加其他HTML标记来操纵输出的地方。因为例如我需要为子元素添加< a>和< img>。

以下是我的最终结果:

 <   xsl:  style   sheet     version   =  1.0    xmlns:xsl   =  http://www.w3.org/1999/XSL/Transform >  

< xsl:output 方法 = html 缩进 = / >

< xsl:template 匹配 = < span class =code-keyword> * [*]
>
< li >
< ; xsl:value-of select = local-name() / >
< ul >
< < span class =code-leadattribute> xsl:apply-templates / >
< / ul < span class =code-keyword>>
< / li >
< / xsl:模板 >

< xsl:template match = * [not(*)] >
< span class = code-keyword>< li >
< xsl:value-of 选择 = local-name() / >
< / li >
< / xsl:template >

< xsl:template 匹配 = / * priority = 5 >
< ul >
< xsl:apply-templates / >
< / ul >
< / xsl:template >

< / xsl: style sheet >


I've got quite some challenge in here...

Is it possible to have an XSL code that will apply to whatever hierarchy of XML, to transform it into nested HTML list?

Example XML:

<Base>
  <Parent>
    <Child />
    <Child />
    <Child>
      <ChildOne />
      <ChildOne />
      <ChildOne>
       <ChildTwo />
       <ChildTwo />
      </ChildOne>
    </Child>
  </Parent>
  <Parent>
    <Child>
      <ChildOne>
        <ChildTwo>
          <ChildThree>
            <ChildFour />
          </ChildThree>
          <ChildThree/>
        </ChildTwo>
      </ChildOne>
      <ChildOne/>
      <ChildOne/>
    </Child>
    <Child/>
    <Child/>
  </Parent>
</Base>


Desired result:

<ul>
  <li>Parent1
    <ul>
      <li>Child</li>
      <li>Child
        <ul>
        <li>ChildOne</li>
        <li>ChildOne>
           <ul>
              <li>ChildTwo</li>
              <li>ChildTwo</li>
           </ul>
        </li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Parent2
    <ul>
      <li>Child
        <ul>
            <li>ChildOne
                <ul>
                    <li>ChildTwo
                  <ul>
                  <li>ChildThree
                     <ul>
                         <li>ChildFour</li>
                     </ul>
                  </li>
                  <li>ChildThree</li>
                  </ul>
                </li>
            </ul>
            </li>
            <li>ChildOne</li>
            <li>ChildOne</li>
         </ul>
       </li>
       <li>Child</li>
    </ul>
  </li>
</ul>


So the logic is that if there is other elements(suppressed el.) under an element(upper el.) - the suppressed el. get surround by <li>name of upper el.<ul>all suppressed el.</ul></li> and so on.

My point is that this XSL code works not by values or names of elements or attributes, but by condition checking, if there are other elements falling under a particular element.

F.e., if ChildThree didn't have ChildFour</li> under itself, it would just be <li>ChildThree> instead of

<li>ChildThree
   <ul>
      <li>ChildFour</li>
   </ul>
</li>


Hope I explained myself clear. :)

解决方案

This gets you close without much effort. Not sure how/why you're filtering out some of your elements, and your use of an index in your desired output is inconsistent, so left that out:

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

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">
      <ul>
        <xsl:for-each select="*">
          <li>
            <xsl:value-of select="name(.)"/>
            <xsl:if test="count(*) > 0">
              <xsl:apply-templates select="."></xsl:apply-templates>
            </xsl:if>
          </li>
        </xsl:for-each>
      </ul>
    </xsl:template>
</xsl:stylesheet>


The above solution is working fine, but it doesn't leave place for manipulating the output by adding other HTML tags. Because for example I need to put, for the child elements, <a> and <img>.
Here is what I ended up with:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="yes" />

<xsl:template match="*[*]">
  <li>
    <xsl:value-of select="local-name()" />
    <ul>
      <xsl:apply-templates />
    </ul>
  </li>
</xsl:template>

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

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

</xsl:stylesheet>


这篇关于用于转换的通用XSLT代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆