将命名空间从 Java 传递到 xslt,并使用来自 Java 的参数作为 xslt 中的节点

我有一个xslt文件,使用阿帕奇-福普将xml文件转换为pdf。但是我的xslt中没有关于命名空间的所有信息。这取决于 xml。我可以在java中分析xml文档并从xml获取所有命名空间。但是我不知道如何将这个命名空间从java传递到我的xslt文件,以及接下来如何在标签中声明它。可能吗?<xsl:stylesheet>


我无法粘贴我的原始 xslt 和 xml,因为它有敏感数据,但我准备了示例文件来显示我的问题:


    <?xml version="1.0" encoding="UTF-8"?>

<ns0:OtherCompany xmlns:ns8="http://www.company.com/schema/SF/definition/type/test"  xmlns:ns0="http://www.company.com/schema/SF/definition/type/a" xmlns:ns7="http://www.company.com/schema/SF/definition/type/b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <ns0:Header>

      <ns8:From>2018-01-01</ns8:From>

      <ns8:To>2018-12-31</ns8:To>

      <ns8:CheckDate>2019-03-28</ns8:CheckDate>

      <ns7:Code sysCode="1">Report</ns7:Code>

      <ns7:Type>1</ns7:Type>

   </ns0:Header>

   <ns0:Changes>

      <ns7:I>

         <ns8:AmountA>1499142.61</ns8:AmountA>

         <ns8:AmountB>54979.16</ns8:AmountB>

      </ns7:I>

      <ns7:II>

         <ns8:AmountA>3398983.19</ns8:AmountA>

         <ns8:AmountB>1499142.61</ns8:AmountB>

      </ns7:II>

      <ns7:III>

         <ns8:AmountA>3398983.19</ns8:AmountA>

         <ns8:AmountB>1499142.61</ns8:AmountB>

      </ns7:III>

   </ns0:Changes>

</ns0:OtherCompany>

如何从 Java 传递 xmlPath前缀并在我的 xslt 中使用它?我想将示例字符串作为 xml 路径前缀


“ns0:其他公司/ns0:更改”


第二个问题是我的命名空间,pathPrefix可以不同,但本地名称总是一样的,例如它可以是:


"ns0:OtherCompany/ns0:Changes"

"ns10:OtherCompany/ns15:Changes"

"companyType:OtherCompany/companyChanges:Changes"

或更多其他选项。当我有 xslt 时,我必须在示例 ns0、ns10、公司类型等中声明标记。如果我不声明它,我就会出错。但我不知道我的xml中声明了什么命名空间。我该如何将其传递给 xslt?<xsl:stylesheet>


示例 i 通行证


xmlPath前缀: “ns10:其他公司/ns15:Changes”


和命名空间:ns10 和 ns15


但我不知道如何达到它。


FFIVE
浏览 68回答 1
1回答

慕慕森

您是否使用像萨克森 9 这样的 XSLT 2 处理器?您的 XSLT 代码显示 。如果您正在处理各种命名空间,那么在 XSLT/XPath 2 及更高版本中,一种方法是对命名空间前缀使用通配符,例如 将选择任何命名空间中的这些元素。version="2.0"**:OtherCompany/*:Changes要参数化表达式,您需要使用 XSLT 3 处理器(如 Saxon 9.8 或 9.9)以及所谓的阴影属性(如 静态参数):select_select<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&nbsp; &nbsp; xmlns:xs="http://www.w3.org/2001/XMLSchema"&nbsp; &nbsp; exclude-result-prefixes="#all"&nbsp; &nbsp; version="3.0">&nbsp; <xsl:param name="prefix" as="xs:string" static="yes" select="'/*:root/*:foo'"/>&nbsp; <xsl:output indent="yes"/>&nbsp; <xsl:template match="/">&nbsp; &nbsp; &nbsp; <xsl:apply-templates _select="{$prefix}/*:bar"/>&nbsp; </xsl:template>&nbsp; <xsl:template match="*:bar">&nbsp; &nbsp; &nbsp; <xsl:copy-of select="."/>&nbsp; </xsl:template></xsl:stylesheet>https://xsltfiddle.liberty-development.net/ej9EGco我认为,您需要使用萨克森的s9api编程接口来设置静态参数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java