2017-04-06 1 views
0

http://saxon.sourceforge.net/#F9.7HEトランスHTML(XSL 2.0)

http://xslttest.appspot.com/でxslスクリプトを実行すると、エラーは発生せず、出力は正しいです。

HTMLコード

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> 
    <head> 
     <title>Test Title</title> 
    </head> 
    <body> 
    <h1>Test Header</h1> 
    <p>Blah Blah Blah</p> 
     <p class="center"><img src="ignore.jpeg" alt="ignore"/></p> 
    <div class="Test"><p>More Text</p></div> 
    </body> 
</html> 

XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <xsl:output method="text" media-type="text"/> 

    <xsl:template match="/xhtml:html"> 
     <xsl:call-template name="print-it"> 
      <xsl:with-param name="nodeToPrint" select="xhtml:body"/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="print-it"> 
     <xsl:param name="nodeToPrint"/> 
     <xsl:for-each select="child::*"> 
      <xsl:choose> 
       <xsl:when test="matches(lower-case(local-name(.)), 'h[123456]|p|div|title')"> 
        <xsl:value-of select="concat(normalize-space(replace(string-join(text(), ''), '''', '')), ' ')"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="normalize-space(replace(string-join(text(), ''), '''', ''))"/> 
       </xsl:otherwise> 
      </xsl:choose> 
      <xsl:call-template name="print-it"> 
       <xsl:with-param name="nodeToPrint" select="."/> 
      </xsl:call-template> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

出力:しかし

Test Title 


Test Header 
Blah Blah Blah 

More Text 

、私がやろう.NETでの変換、私は例外を取得します。私は、問題がXSLスクリプトであり、オンラインコンバータが許しているのか、Saxonライブラリがボールを落としているのかはわかりません。

例外メッセージ:

Exception thrown: 'System.InvalidOperationException' in saxon9he.dll 

Additional information: The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type. 

.NETコード

using Saxon.Api; 

var xslt = new FileInfo(@"C:\path\to\stylesheet.xslt"); 
var input = new FileInfo(@"C:\path\to\data.xml"); 
var output = new FileInfo(@"C:\path\to\result.xml"); 

// Compile stylesheet 
var processor = new Processor(); 
var compiler = processor.NewXsltCompiler(); 
var executable = compiler.Compile(new Uri(xslt.FullName)); 

// Do transformation to a destination 
var destination = new DomDestination(); 
using(var inputStream = input.OpenRead()) 
{ 
    var transformer = executable.Load(); 
    transformer.SetInputStream(inputStream, new Uri(input.DirectoryName)); 
    transformer.Run(destination); 
} 

// Save result to a file (or whatever else you wanna do) 
destination.XmlDocument.Save(output.FullName); 

UPDATE

はあなたにMartinHonnenをありがとうございます。あなたの提案はうまくいった。

Serializer _serializer = new Serializer(); 
MemoryStream _ms = new MemoryStream(); 
String _outputStream = new StreamWriter(_ms, new UTF8Encoding(false)); 
_serializer.SetOutputWriter(_outputStream); 

using (inputStream == input.OpenRead()) { 
    XsltTransformer transformer = executable.Load(); 
    transformer.MessageListener = new SaxtonMessageListener(); 
    transformer.SetInputStream(inputStream, new Uri(input.DirectoryName)); 
    transformer.Run(_serializer); 
} 

String _text = Encoding.UTF8.GetString(_ms.ToArray()); 
+0

例外はどの行にスローされますか? –

+0

まあ、問題は、あなたのスタイルシートが、DOM宛先に収まるように整形式のXML文書を作成するのではなく、テキストだけを含むフラグメントを作成することです。 XSLT 2.0とSaxonを必要としているかどうかはわかりません。入力には名前空間の要素がなく、スタイルシートがXHTML名前空間で一致するものの、スタイルシートのテンプレートは使用されていないので、その結果はhttp://saxonica.com/html/documentation/dotnetdoc/Saxon/Api/DomDestination.html#DomDestination(System.Xml.XmlDocumentFragment)。 DOMが必要かどうかは不明です。 –

+0

@Örvarトランスフォーマー上。ラン(目的地);行 – user3541092

答えて

1

あなたは、単に文字列またはテキストファイルをしたい場合は、あなたがhttp://saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Serializer.htmlを使用することができますし、ファイルをしたい場合はどちらかがhttp://saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Serializer.html#SetOutputFile(string)を設定したり、StringWriterを作成し、Runは、あなただけの必要を呼び出し、その後の後、http://saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Serializer.html#SetOutputWriter(System.IO.TextWriter)に渡します私が思うに、作成したStringWriterにToString()を使用すると思います。