开发者

How to set XSLT in C# to ignore error

开发者 https://www.devze.com 2022-12-19 08:21 出处:网络
Is there a way to turn off XSLT compile errors when creating HTML from an xsl stylesheet. For instance, for every html tag is expecting a closing tag.

Is there a way to turn off XSLT compile errors when creating HTML from an xsl stylesheet.

For instance, for every html tag is expecting a closing tag.

Now the final doc does have closing tags but it is within muliple templates where one would have the open, the last would close it. Is there a way to surpress the xslt compile error?

I am using the XslCompiledTransform Class.

my XSLT file:

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

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

  <xsl:key name="categories" match="Category" use="." /> 
  <xsl:key name="client" match="Client" use="." /> 
  <xsl:key name="title" match="Title" use="." /> 

  <xsl:template match="/"> 
    <ul id="red" class="treeview-red"> 
      <xsl:for-each select="/Promotions/Promotion/Category[  
                generate-id(.) = generate-id(key('categories', .)[1])  
                ]">
      <li>
    <span>
      <xsl:value-of select="."/> 
    </span>

    <ul> 
      <xsl:call-template name="category-client"> 
        <xsl:with-param name="category" select="."/> 
      </xsl:call-template> 
    </ul>
  </li>

    </xsl:for-each>
    </ul>
  </xsl:template> 

  <xsl:template name="category-client"> 
    <xsl:param name="category" /> 
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Client[  
                generate-id(.) = generate-id(key('client', .)[1]) 
                ]">
      <li>
        <span>
          <xsl:value-of select="."/>
        </span>

        <ul>


          <xsl:call-template name="category-client-title">
            <xsl:with-param name="category" select="$category"/>
            <xsl:with-param name="client" select="."/>
          </xsl:call-template>


        </ul>
      </li>
    </xsl:for-each> 
  </xsl:template> 

  <xsl:template name="category-client-title"> 
    <xsl:param name="category" /> 
    <xsl:param name="client" /> 
    <xsl:for-each select="/Promotions/Promotion[Category=$category]/Title[  
                generate-id(.) = generate-id(key('title', .)[1]) 
                ]"> 
      <li> 
    <span> 
      <xsl:value-of select="."/> 
    </span> 
      </li> 

    </xsl:for-each> 
  </xsl:template> 

</xsl:stylesheet> 

My XML:

<Promotions> 
  <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 2</Title> 
  </Promotion> 
  <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 4</Title> 
  </Promotion> 
  <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 5</Title> 
  </Promotion> 
  <Promotion> 
    <Category>Community &amp; Neighborhood</Category> 
    <Client>Client 2</Client> 
    <Title>Get your Free 1</Title> 
  </Promotion> 
  <Promotion> 
    <Category>Education</Category> 
    <Client>Client 3</Client> 
    <Title>Get Your Free 3</Title> 
  </Promotion> 
</Promotions> 

I would like the output structured like this:

<ul id='red' class='treeview-red'>
 <li><span>Item 1</span>
  <ul>
   <li><span>Item 1.0</span>
    <ul>
     <li><span>Item 1.0.0</span></li>
    </ul>
   </li>
   <li><span>Item 1.1</span></li>
   <li><span>Item 1.2</span>
    <ul>
     <li><span>Item 1.2.0</span>
     <ul>
      <li><span>Item 1.2.0.0</span></li>
      <li><span>Item 1.2.0.1</span></li>
      <li><span>Item 1.2.0.2</span></li>
     </ul>
    </li>
     <li><span>Item 1.2.1</span>
     <ul>
      <li><span>Item 1.2.1.0</span></li>
     </ul>
    </li>
     <li><span>Item 1.2.2</span>
     <ul>
      <li><span>Item 1.2.2.0</span></li>
      <li><span>Item 1.2.2.1</span></li>
      <li><span>Item 1.2.2.2</span></li>
     </ul>
    </li>
    </ul>
   </li>
  </ul>
 </li>
 <li><span>Item 2</span>
  <ul>
   <li><span>Item 2.0</span>
    <ul>
     <li><span>Item 2.0.0</span>
     <ul>
      <li><span>Item 2.0.0.0</span></li>
      <li><span>Item 2.0.0.1</span></li>
     </ul>
    </li>
    </ul>
   </li>
   <li><span>Item 2.1</span>
    <ul>
     <li><span>Item 2.1.0</span>
     <ul>
      <li><span>Item 2.1.0.0</span></li>
     </ul>
    </li>
     <li><span>Item 2.1.1</span>
     <ul>
      <li><span>Item 2.1.1.0</span></li>
      <li><span>Item 2.1.1.1</span></li>
      <li><span>Item 2.1.1.2</span></li>
     </ul>
    </li>
     <li><span>Item 2.1.2</span>
     <ul>
      <li><span>Item 2.1.2.0</span></li>
      <li><span>Item 2.1.2.1</span></li>
      <li><span>Item 2.1.2.2</span></li>
     </ul>
    </li>
    </ul>
   </li>
  </ul>
 </li>
 <li class='open'><span>Item 3</span>
  <ul>
   <li class='open'><span>Item 3.0</span>
    <ul>
     <li><span>Item 3.0.0</span></li>
     <li><span>Item 3.0.1</span>
     <ul>
      <li><span>Item 3.0.1.0</span></li>
      <li><span>Item 3.0.1.1</span></li>
     </ul>

    </li>
     <li><span>Item 3.0.2</span>
     <ul>
      <li><span>Item 3.0.2.0</span></li>
      <li><span>Item 3.0.2.1</span></li>
      <li><span>Item 3.0.2.2</span></li>
     </ul>
    </li>
    </ul>
   </li>
  </ul>
 </li>
 </ul>";

to use in this control:

like sample 2:

http://jquery.bassistance.de/treeview/demo/

my current results, close but not right yet:

        <ul id="red" class="treeview-red">
  <li><span>Arts &amp; Entertainment</span><ul>
      <li><span>Client 1</span><ul>
          <li><span>Get your Free 2</span></li>
          <li><span>Get your Free 4</span></li>
          <li><span>Get your Free 5</span></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><span>Community &amp; Neighborhood</span><ul>
      <li><span>Client 2</span><ul>
     开发者_Python百科     <li><span>Get your Free 1</span></li>
        </ul>
      </li>
    </ul>
  </li>
  <li><span>Education</span><ul>
      <li><span>Client 3</span><ul>
          <li><span>Get Your Free 3</span></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

If I could close this tag at the end

</ul> 

Then it would be fine, but when I try I get error.


This is a common question on the xsllist forum. But the answer is no.

When using XSLT, you cannot think serially of emitted "tags" but instead you have to think of writing elements and their content. So you don't start here, and end there, but you have to open and close together and call elsewhere for the intervening content.

It's a hard concept to grasp; at first it's like writing code while standing on your head.

If you have an example, it would be easier to help.


If you need to generate unmatched tags, then you can output them by wrapping them in these tags:

<xsl:text disable-output-escaping="yes"><![CDATA[
  any HTML in here will not be validated
]]></xsl:text>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号