标签: saxon

XSLT:将分组html元素移动到节级别

我正在尝试编写一个XSLT,根据标题级别将HTML文件组织到不同的部分级别.这是我的意见:

<html>
 <head>
  <title></title>
 </head>
 <body>
  <h1>HEADER 1 CONTENT</h1>
  <p>Level 1 para</p>
  <p>Level 1 para</p>
  <p>Level 1 para</p>
  <p>Level 1 para</p>

  <h2>Header 2 CONTENT</h2>
  <p>Level 2 para</p>
  <p>Level 2 para</p>
  <p>Level 2 para</p>
  <p>Level 2 para</p>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我目前正在使用一个相当简单的结构,所以这种模式将暂时保持不变.我需要这样的输出......

<document> 
  <section level="1">
     <header1>Header 1 CONTENT</header1>
     <p>Level 1 para</p>
     <p>Level 1 para</p>
     <p>Level 1 para</p>
     <p>Level 1 para</p>
     <section level="2">
        <header2>Header 2 CONTENT</header2>
        <p>Level 2 para</p>
        <p>Level 2 para</p>
        <p>Level 2 para</p>
        <p>Level 2 para</p>
     </section>
  </section>
</document>
Run Code Online (Sandbox Code Playgroud)

我一直在使用这个例子:Stackoverflow答案

但是,我无法让它完全按照我的需要去做. …

html xslt grouping saxon

6
推荐指数
2
解决办法
1458
查看次数

如何在xquery中格式化小数?

我正在尝试在XQuery中格式化小数.小数是货币,因此格式应该是,###.##.

例如:

5573652.23 应该 5,573,652.23

352769应该352,769(或者352,769.00如果它更容易/更清洁)

现在我正在使用http://www.xqueryhacker.com/2009/09/format-number-in-xquery/中的这个函数,但我不能用它来使用小数:

declare function local:format-int($i as xs:int) as xs:string
{
  let $input :=
    if ($i lt 0) then fn:substring(fn:string($i), 2)
    else fn:string($i)
  let $rev := fn:reverse(fn:string-to-codepoints(fn:string($input)))
  let $comma := fn:string-to-codepoints(',')

  let $chars :=
    for $c at $i in $rev
    return (
      $c,
      if ($i mod 3 eq 0 and fn:not($i eq count($rev)))
      then $comma else ()
    )

  return fn:concat(
    if ($i lt 0) then '-' …
Run Code Online (Sandbox Code Playgroud)

xquery saxon number-formatting

6
推荐指数
1
解决办法
4893
查看次数

应用程序使用saxonHE(9.2.1.1)api处理针对多个XML文件的XSLT(v2.0)

我有一个应用程序使用SAXONHE 9.2.1.1 api文件将XML数据转换为纯文本.我的表单有文本框

  1. XMLInput_FilePath
  2. XSLT_FilePath
  3. TextOutput_FilePath

在我的表单的okButton_Click()事件,我有以下内容:

private void okButton_Click(object sender, EventArgs e) {
    FileStream xsltTransform_FileStream = File.Open(xsltTransform_FilePath.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    FileStream xmlInput_FileStream = File.Open(xmlInput_FilePath.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    XmlTextReader modelFileXML = new XmlTextReader(xmlInput_FileStream);
    modelFileXML.XmlResolver = null;

    Processor processor = new Processor();

    XdmNode input = processor.NewDocumentBuilder().Build(modelFileXML);

    XsltTransformer xsltTransformer = processor.NewXsltCompiler().Compile(xsltTransform_FileStream).Load();
    xsltTransformer.InputXmlResolver = null;
    xsltTransformer.InitialContextNode = input;

    Serializer serializer = new Serializer();
    serializer.SetOutputFile(writeFile);

    xsltTransformer.Run(serializer);

    xsltTransform_FileStream.Close();
    modelFileStream.Close();
}
Run Code Online (Sandbox Code Playgroud)

在我的XMLInput文件的上下文中,有一个对另一个XML文件中的数据的引用 - 见下文:

XML:

<XMLInput_File
  Name="XMLInput_File">
  <Subsystem Name="Subsystem">
    <Requirements Name="Requirement_1">
      <Rows>
        <Path Text="XMLInput2_File:/XMLInput2_File/Subsystem_1/Field_1" />
      </Rows> …
Run Code Online (Sandbox Code Playgroud)

c# xslt saxon .net-3.5 winforms

6
推荐指数
1
解决办法
742
查看次数

查找XML文档中的所有名称空间声明 - xPath 1.0 vs xPath 2.0

作为Java 6应用程序的一部分,我想在XML文档中查找所有名称空间声明,包括任何重复项.

编辑:Per Martin的请求,这是我正在使用的Java代码:

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = xPathExpression = xPath.compile("//namespace::*"); 
NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDomDocument, XPathConstants.NODESET);
Run Code Online (Sandbox Code Playgroud)

假设我有这个XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ele="element.com" xmlns:att="attribute.com" xmlns:txt="textnode.com">
    <ele:one>a</ele:one>
    <two att:c="d">e</two>
    <three>txt:f</three>
</root>
Run Code Online (Sandbox Code Playgroud)

要查找所有名称空间声明,我使用xPath 1.0将此xPath语句应用于XML文档:

//namespace::*
Run Code Online (Sandbox Code Playgroud)

它找到4个名称空间声明,这是我期望(和期望):

/root[1]/@xmlns:att - attribute.com
/root[1]/@xmlns:ele - element.com
/root[1]/@xmlns:txt - textnode.com
/root[1]/@xmlns:xml - http://www.w3.org/XML/1998/namespace
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改为使用XPath 2.0,然后我得到16点命名空间声明(每个先前声明的4倍),这不是我所期望(或希望):

/root[1]/@xmlns:xml - http://www.w3.org/XML/1998/namespace
/root[1]/@xmlns:att - attribute.com
/root[1]/@xmlns:ele - element.com
/root[1]/@xmlns:txt - textnode.com
/root[1]/@xmlns:xml - http://www.w3.org/XML/1998/namespace
/root[1]/@xmlns:att - attribute.com
/root[1]/@xmlns:ele - …
Run Code Online (Sandbox Code Playgroud)

java xml xpath saxon xml-namespaces

6
推荐指数
1
解决办法
7758
查看次数

为什么我需要在RegEx中使用双卷曲括号?

我在我的一个xsl-transformations(xsl:analyze-string)中运行了一个小正则表达式并遇到了这个让我感到不舒服的效果,因为我没有找到任何解释......

我正在寻找Non-Breaking-Spaces和En-Spaces,所以我使用了\p{Z}构造.根据Michael Kay的XSLT 2.0程序员参考中的许多示例,这应该可行.RegExBuddy也批准:)

现在我的SaxonHE9.4N告诉我了

正则表达式中的错误:net.sf.saxon.trans.XPathException:expected({)

经过几次试验和错误后,我简单地将支架加倍\p{{Z}}......它有效!?但这次RegExBuddy不赞成!

有人可以给我解释一下这种效果吗?我在互联网上找不到任何令人满意的东西......

提前致谢!

编辑:我在replace()函数内部尝试了同样的事情,双括号版本不起作用.我不得不用单支架做到这一点!

regex xslt saxon

6
推荐指数
1
解决办法
1414
查看次数

XSLT 3.0流(Saxon)

我有一个带有这种树的大XML文件(6 GB):

<Report>
   <Document>
      <documentType>E</documentType>
      <person>
         <firstname>John</firstname>
         <lastname>Smith</lastname>
      </person>
   </Document>
   <Document>
      [...]
   </Document>
   <Document>
      [...]
   </Document>
   [...]
</Report>
Run Code Online (Sandbox Code Playgroud)

如果在其上应用XSLT样式表,则会出现此错误:

线程“主”中的异常java.lang.OutOfMemoryError:Java堆空间

因此,我想尝试XSLT 3.0的新功能:使用Saxon 9.6 EE进行流传输。我不想一次在文档中使用流式传输约束。我认为我想做的非常类似于此处描述的“突发模式”:http : //saxonica.com/documentation/html/sourcedocs/streaming/burst-mode-streaming.html

这是我的Saxon命令行:

java -cp saxon9ee.jar net.sf.saxon.Transform -t -s:input.xml -xsl:stylesheet.xsl -o:output / output.html

这是我的XSLT样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:mode streamable="yes" />

<xsl:template match="/">
    GLOBAL HEADER
        <xsl:iterate select="copy-of()/Report/Document" >
           DOC HEADER
           documentType: <xsl:value-of select="documentType"/>
           person/firstname: <xsl:value-of select="person/firstname"/>
           DOC FOOTER
           <xsl:next-iteration/>
        </xsl:iterate>
    GLOBAL FOOTER
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

但是我仍然有同样的内存不足错误。

谢谢您的帮助!

xml xslt streaming saxon xslt-3.0

6
推荐指数
1
解决办法
1566
查看次数

使用saxon和python

我需要使用python处理XSLT,目前我正在使用仅支持XSLT 1的lxml,现在我需要处理XSLT 2有没有办法在python中使用saxon XSLT处理器?

python xslt saxon xslt-2.0

6
推荐指数
4
解决办法
6277
查看次数

xslt:Saxon 9.4与Saxon 9.6的消息

我正在尝试从使用Saxon时包含来自xsl:message标签的消息的java代码中抛出异常.

使用以下xslt文件

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:message terminate="yes">exception message</xsl:message>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

在Saxon 9.4上使用以下代码

public static void main(String[] args) throws TransformerException {
    try {
        TransformerFactory fact = new net.sf.saxon.TransformerFactoryImpl();
        Transformer newTransformer = fact.newTransformer(new StreamSource(new File("throw.xslt")));
        ((net.sf.saxon.Controller)newTransformer).setRecoveryPolicy(Configuration.DO_NOT_RECOVER);
        ((net.sf.saxon.Controller)newTransformer).setMessageEmitter(new MessageWarner());
        newTransformer.transform(new StreamSource(new File("input.xml")), new StreamResult(new File("output.xml")));
    } catch (TransformerException e) {
        System.out.println("THIS IS EXCEPTION: " + e.getMessage() + " <<<");
        throw e;
    }
}   
Run Code Online (Sandbox Code Playgroud)

它给出了THIS IS EXCEPTION: exception message <<<,这是我期待的行为.

但是对于Saxon 9.6而言,由于API的变化而调整了一些代码

public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java xml xslt saxon

6
推荐指数
2
解决办法
628
查看次数

如何使用 XQuery 从 Java 使用 Saxon 输出 json

我使用 XQuery 将下面的 XML 文档转换为 JSON,并使用 Saxon 处理 XQuery 文件。

<books>
   <book id="book1">
      <author>Book1AuthorSurname, Book1AuthorFirstname</author>
      <title>Book1 Title</title>
      <price>45.00</price>
   </book>
   <book id="book2">
      <author>Book2AuthorSurname, Book2AuthorFirstname</author>
      <title>Book2 Title</title>
      <price>45.00</price>
   </book>
</books>
Run Code Online (Sandbox Code Playgroud)

XQuery 本身非常简单

xquery version "3.1";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization"; 
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";

declare option output:method "json";
declare option output:indent "yes";

let $booksXML := doc("books.xml")/books

return array {
  for $book in $booksXML/book
  return map {
   "author": data($book/author),
   "title": data($book/title)
  }
}
Run Code Online (Sandbox Code Playgroud)

并且,如果我使用 Saxon 从命令行运行它,则会正确返回

java -cp Saxon-HE-9.8.0-8.jar net.sf.saxon.Query -q:books2json.xqy

 [ …
Run Code Online (Sandbox Code Playgroud)

java xquery saxon

6
推荐指数
1
解决办法
1546
查看次数

XSLT3 可以在与 expand-text=yes 相同的模板中使用禁用输出转义吗?

我注意到在 Saxon 的 XSLT3 中尝试使用禁用输出转义时,如果在样式表或什至在给定的匹配模板上将扩展文本设置为 yes,它将不起作用

以下代码(在其自身上运行时)显示了该问题(在 Saxon 9.8.0.12 中)。我知道这是一个不寻常的组合,并且通常不惜一切代价避免禁用输出转义,而只是试图确定正确的行为。

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

    <xsl:template match="/">
        <out>
            <xsl:apply-templates/>
        </out>
    </xsl:template>
    <xsl:template match="xsl:stylesheet" expand-text="true">
        <expandtext>
            <count>{count(*)}</count>
            <xsl:text disable-output-escaping="true">&lt;test/&gt;</xsl:text>
        </expandtext>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="xsl:template" expand-text="false">
        <notexpandtext>
            <count>{count(*)}</count>
            <xsl:text disable-output-escaping="true">&lt;test/&gt;</xsl:text>
        </notexpandtext>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

产生

<?xml version="1.0" encoding="UTF-8"?>
<out>
    <expandtext><count>3</count>&lt;test/&gt;</expandtext>
    <notexpandtext><count>{count(*)}</count><test/></notexpandtext>
    <notexpandtext><count>{count(*)}</count><test/></notexpandtext>
    <notexpandtext><count>{count(*)}</count><test/></notexpandtext>
</out>
Run Code Online (Sandbox Code Playgroud)

xslt saxon xslt-3.0

6
推荐指数
1
解决办法
176
查看次数