XSLT interpret cdata and write to file [closed]

蹲街弑〆低调 提交于 2019-12-13 22:28:24

问题


I have a situation like this:

<List>
     <Entry>
     <![CDATA[......]]>
     </Entry>
     <Entry>
     <![CDATA[......]]>
     </Entry>
     ...
</List>

CDATA sections contain encoded bytes. Each entry's cdata has a control word as the first word, that describes what length of the data will go into a file and how much to seek into that file. For example: 00060000abcdef will imply a file that contains "abcdef" and of length 6 seeking at 0. The output should be a file that contains abcdef.

Is XSLT the right choice to interpret CDATA in a certain way and write to a file?

  1. I can think of using redirect:write as in Xalan
  2. Use some sort of custom XSLT function that will break into a parser handler to handle this:

    <xsl:MyFunc></xsl:MyFunc>
    
  3. Is there a way to write custom XSLT functions as tags using for, if etc.?

Editing after Dimitre Novatchev's feedback:

Thank you for the response! I hope the below clarifies the question. Sorry.

Input XML File:

<File>
  <Chunk>
      <Offset>200</Offset>
      <Length>100</Length>
      <Data>
          <![CDATA[data bytes that can be greater than 100 too, but first 100 is valid.]]>
      </Data>
  </Chunk>
  ...
</File>

The output should be a html file that has this:

<html>
   <body>
       <a href="resulting_file">file</a>
   </body>
</html>

回答1:


  1. Is XSLT the right choice to interpret CDATA in a certain way and write to a file?
  1. Yes, CDATA is just a (part of a) text node and is processed as any other text node.

  2. Yes, the XSLT 2.0 xsl:result-document instruction is used to write to (possibly many different) file(s).

.2. Use some sort of custom XSLT function that will break into a parser handler to handle this:

<xsl:MyFunc></xsl:MyFunc>

Yes, this is done using the standard XSLT 2.0 instruction xsl:function

3.Is there a way to write custom XSLT functions as tags using for, if etc.?

This question isn't clear. XSLT has many such instructions, like xsl:for-each, xsl:if, xsl:choose, xsl:when, xsl:otherwise, ... etc.

XPath 2.0 (which is used within XSLT 2.0) xas its own for, if then else, return clauses.




回答2:


Well for XSLT it doesn't matter whether your XML input has CDATA sections or not, the XSLT data model is a tree of nodes so there will be Entry elements with a single child text node containing the text (e.g. with your sample

 <Entry>
 <![CDATA[......]]>
 </Entry>

an Entry element will contain a single text node containing a line break followed by some space characters followed by ...... followed by a line break followed by some space characters).

You can then process the text node's string value with the XSLT and XPath string functions.



来源:https://stackoverflow.com/questions/12438865/xslt-interpret-cdata-and-write-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!