- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 xsl 工作表将纯文本转换为 svg。我从用户输入到我的 servlet 中获取纯文本,之后我想要将输入作为参数传递给我的 xsl 表。我的最后一个问题是如何传递参数并在这里得到解答(Pass user input to XSL stylesheet),但我意识到我不知道如何打电话转变。我的 xsl 不接受 xml 输入,因此我无法使用 Transformer.transform 等。怎么可能使用参数运行转换并以字符串形式获取结果,我可以将其返回给用户?
如有任何帮助,我们将不胜感激。如果您需要更多详细信息,请告诉我。谢谢!
编辑:如果您有兴趣,我的想法是我可以将纯文本表示法转换为 svg,转换的第一部分将纯文本转换为 xml 表示法,然后将 xml 转换为 svg( XSLT 2.0: Transform notation in plain text to svg )。当纯文本位于变量中时,它适用于简单符号。但我想扩展一个具有该功能的 Web 服务,并且用户应该可以提供输入,这就是为什么我想将参数传递给我的 xsl。问题是我不知道如何从 java 调用转换来获取结果并将其返回给用户。
这是 xsl:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Ansatz mit Hilfe von
https://stackoverflow.com/questions/34682331/xslt-2-0-transform-notation-in-plain-text-to-svg -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:local="local"
exclude-result-prefixes="xs"
version="2.0"
xmlns:svg="http://www.w3.org/2000/svg">
<xsl:output indent="yes"/>
<!--
<xsl:variable name="drawing-text">
GRAPHREP
PEN color:$000000 w:2pt
FILL color:$ff7f00
ROUNDRECT x:0pt y:0pt w:114pt h:70pt rx:20pt ry:20pt
</xsl:variable>
-->
<xsl:param name="drawing-text" />
<!--matches sequences of UPPER-CASE letters -->
<xsl:variable name="label-pattern" select="'[A-Z]+'"/>
<!--matches the "attributes" in the line i.e. w:2pt,
has two capture groups (1) => attribute name, (2) => attribute value -->
<xsl:variable name="attribute-pattern" select="'\s?(\S+):(\S+)'"/>
<!--matches a line of data for the drawing text,
has two capture groups (1) => label, (2) attribute data-->
<xsl:variable name="line-pattern" select="concat('(', $label-pattern, ')\s(.*)\n?')"/>
<!-- Text in quotes holen-->
<xsl:variable name="text-pattern" select="'"(.*?)"'"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<svg width="640" height="480">
<g>
<!-- Find the text patterns indicating the shape -->
<!--Replaced unparsed-text() with local variable for testing
select="unparsed-text('drawing.txt')" -->
<xsl:analyze-string select="$drawing-text"
regex="{concat('(', $label-pattern, ')\n((', $line-pattern, ')+)\n?')}">
<xsl:matching-substring>
<!--Convert text to XML -->
<xsl:variable name="drawing-markup" as="element()">
<!--Create an element for this group, using first matched pattern as the element name
(i.e. GRAPHREP => <GRAPHREP>) -->
<xsl:element name="{regex-group(1)}">
<!--split the second matched group for this shape into lines by breaking on newline-->
<xsl:variable name="lines" select="tokenize(regex-group(2), '\n')"/>
<xsl:for-each select="$lines">
<!--for each line, run through this process to create an element with attributes
(e.g. FILL color:$frf7f00 => <FILL color=""/>
-->
<xsl:analyze-string select="." regex="{$line-pattern}">
<xsl:matching-substring>
<!--create an element using the UPPER-CASE label starting the line -->
<xsl:element name="{regex-group(1)}">
<!-- capture each of the attributes -->
<xsl:analyze-string select="regex-group(2)" regex="\s?(\S+):(\S+)">
<xsl:matching-substring>
<!--convert foo:bar into attribute foo="bar",
translate $ => #
and remove the letters 'p' and 't' by translating into nothing"-->
<xsl:attribute name="{regex-group(1)}" select="translate(regex-group(2), '$pt', '#')"/>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</xsl:element>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</xsl:for-each>
</xsl:element>
</xsl:variable>
<!--Uncomment the copy-of below if you want to see the intermediate XML $drawing-markup-->
<!--<xsl:copy-of select="$drawing-markup"/>-->
<!-- Transform XML into SVG -->
<xsl:apply-templates select="$drawing-markup"/>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</g>
</svg>
</xsl:template>
<!--==========================================-->
<!-- Templates to convert the $drawing-markup -->
<!--==========================================-->
<!--for supported shapes, create the element using
lower-case value, and change rectangle to rect
for the svg element name !!! if abfrage ob text-->
<xsl:template match="GRAPHREP[ELLIPSE | RECTANGLE | ROUNDRECT | LINE | TEXT]">
<xsl:if test="ELLIPSE | RECTANGLE | ROUNDRECT | LINE">
<xsl:element name="{replace(lower-case(local-name(ELLIPSE | RECTANGLE | ROUNDRECT | LINE)), 'rectangle|roundrect', 'rect', 'i')}">
<xsl:attribute name="id" select="concat('id_', generate-id())"/>
<xsl:apply-templates />
</xsl:element>
</xsl:if>
<xsl:if test="TEXT">
<xsl:element name="{lower-case(local-name(TEXT))}">
<xsl:attribute name="id" select="concat('id_', generate-id())"/>
<xsl:apply-templates />
<!-- Da muss der text aus den quotes rein -->
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="ELLIPSE | RECTANGLE | ROUNDRECT | LINE | TEXT"/>
<!-- Just process the content of GRAPHREP.
If there are multiple shapes and you want a new
<svg><g></g></svg> for each shape,
then move it from the template for "/" into this template-->
<xsl:template match="GRAPHREP/*">
<xsl:apply-templates select="@*"/>
</xsl:template>
<xsl:template match="PEN" priority="1">
<!--TODO: test if these attributes exist, if they do, do not create these defaults.
Hard-coding for now, to match desired output, since I don't know what the text
attributes would be, but could wrap each with <xsl:if test="not(@dasharray)">-->
<xsl:attribute name="stroke-dasharray" select="'null'"/>
<xsl:attribute name="stroke-linjoin" select="'null'"/>
<xsl:attribute name="stroke-linecap" select="'null'"/>
<xsl:apply-templates select="@*"/>
</xsl:template>
<!-- conterts @color => @stroke -->
<xsl:template match="PEN/@color">
<xsl:attribute name="stroke" select="."/>
</xsl:template>
<!--converts @w => @stroke-width -->
<xsl:template match="PEN/@w">
<xsl:attribute name="stroke-width" select="."/>
</xsl:template>
<!--converts @color => @fill and replaces $ with # -->
<xsl:template match="FILL/@color">
<xsl:attribute name="fill" select="translate(., '$', '#')"/>
</xsl:template>
<!--converts @h => @font-size !!noch mit text verbinden -->
<xsl:template match="FONT/@h">
<xsl:attribute name="font-size" select="."/>
</xsl:template>
<!--converts @color => @fill !!noch mit text verbinden -->
<xsl:template match="FONT/@color">
<xsl:attribute name="fill" select="translate(., '$', '#')"/>
</xsl:template>
<!-- converts @x => @cx with hard-coded values.
May want to use value from text, but matching your example-->
<xsl:template match="ELLIPSE/@x | ELLIPSE/@y">
<!--not sure if there was a relationship between ELLIPSE x:0pt y:0pt, and why 0pt would be 250,
but just an example...-->
<xsl:attribute name="c{name()}" select="250"/>
</xsl:template>
<xsl:template match="RECTANGLE/@w | ROUNDRECT/@w">
<xsl:attribute name="{name()}idth" select="."/>
</xsl:template>
<xsl:template match="RECTANGLE/@h | ROUNDRECT/@h">
<xsl:attribute name="{name()}eight" select="."/>
</xsl:template>
<xsl:template match="LINE/@x | LINE/@y">
<xsl:attribute name="{name()}" select="."/>
</xsl:template>
<xsl:template match="TEXT/@x | TEXT/@y">
<xsl:attribute name="{name()}" select="."/>
</xsl:template>
</xsl:stylesheet>
最佳答案
在 XSLT 2.0 中,您可以使用命名模板(例如,通过将 <xsl:template match="/">
更改为 <xsl:template match="/" name="main">
),然后,例如在 Saxon 9.6 中,您可以使用方法 http://saxonica.com/html/documentation9.6/javadoc/net/sf/saxon/s9api/XsltTransformer.html#setInitialTemplate%28net.sf.saxon.s9api.QName%29设置myTransformer.setInitialTemplate(new QName("main"))
。请参阅http://saxonica.com/html/documentation9.6/using-xsl/embedding/s9api-transformation.html有关如何使用其 API 的详细信息。
Java JAXP API 面向 XSLT 1.0,不允许您在没有输入文档的情况下运行转换,在那里,您确实需要提供一个虚拟文档。
关于java - 如何在没有从java输入xml的情况下执行xsl转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35382681/
我知道这可能看起来像一个愚蠢/新手问题,但我对 XSLT 还很陌生(尽管我开始接受它并了解它的功能)。 什么时候适合使用xsl:if,什么时候适合使用xsl:choose/xsl:when? 当我想要
在根据节点是否存在设置变量后,我尝试输出变量的文字字符串值。我认为条件检查逻辑是正确的。但它没有输出值... su
我目前正试图使用 XSL 将 XML 文档转换为 HTML。 XML 文档使用 namespace ,我对 XSL 并没有太多经验,更不用说 namespace 了。基本上我想做的就是抓取 s:t
你能否在嵌套的 xsl:when 中嵌套 xsl:if,例如:
我正在 W3Schools 上使用 XSLT 代码示例:https://www.w3schools.com/xml/xsl_client.asp 我有这部分代码在 IE 中使用基本的 xslt 处理.
我正在尝试从 XSLT 样式表动态生成 XSLT 文档。原则上这当然有效,但我没有让命名空间工作。我想让生成的 XSLT 元素以“xsl”前缀为前缀: 而不是 我玩过 xsl:element 的
我们客户的要求是提供带圆角的 PDF 表格。我只有 Apache FOP 处理器可供我使用,它不支持圆角属性。它也不支持 float ,因此无法向左和向右 float 圆形图像。 你对如何做到这一点有
我有一个关于 xsl 的问题。我有 1 个巨大的 xsl 文件(+4000 行 :p),我想将文件分成不同的部分。我使用 xsl 文件来映射 BizTalk 中的一些架构,如果我将它分成几部分,它的性
我的 FO 流程有问题,因为在一页上,标题显示在一页的末尾并在之后的页面上继续。如何向 title-element 添加属性,以便标题始终显示在同一页面上,这意味着:没有分页符。 谢谢! /丹尼 最佳
我想知道是否有人可以帮助我或指出正确的方向。我目前有从 XML 文件返回正确的元素有些麻烦。我只是尝试获取我拥有的大型 XML 文件的精简版本,以便输出另一个 XML 文件(不是很多教程都在使用的 H
我想动态创建具有动态名称的变量以供以后在我的转换中使用,但为此我需要动态生成 XSL,然后在同一脚本中运行它。 这只是我正在寻找的粗略伪代码示例。
我正在学习 XML 以及如何使用 XSL 文件。在 XSL 文件中,我发现了以下术语: xsl:template match="/" 这代表什么?我可以用什么代替 /?我可以编写 table 或任何其
是否可以将 xsl: 放到 XSL 样式表中的 XSL 标签? 例如,可以: 输入为: 编辑:样式表中包含 HTML 和 XML 节点。 谢谢,罗斯 最佳答案 实际上,您可以通过将 XSLT(“h
有什么办法不离开后第一 匹配但继续检查其他条件? 最佳答案 我相信这是一个不。正如规范所说: The content of the first, and only the first, xsl:whe
在构建 XSL 文档时,我遇到了以下问题。我想保留原文中的换行符,所以我设置了linefeed-treatment="preserve" .然而,这显然也意味着它保留了文本内容之外和实际 xml 元素
我有一个 XSL 样式表,我需要使用 xsl:function 添加一些自定义字符串操作。但是我在尝试找出在我的文档中放置该函数的位置时遇到了麻烦。 我的 XSL 简化看起来像这样,
我有以下 XML: Picture 1 Picture 2 Picture 3 虽然此 XSL 执行了预期的操作(输出第一张图片的属性):
我希望 column-count="2"只出现在页面的某些部分。 ]>
我正在为我的 XML 内容生成 xsl-fo 文档,然后将此内容传递给将生成 PDF 的第三方 DLL 之一。我需要以 45 度角显示测试。如何实现这一目标? 谢谢 最佳答案 我很确定您不能在纯 XS
在我的情况下,我必须在两个表格行之间提供高达 0.5cm 的空间。我怎样才能做到这一点。 代码:: 我用过了:
我是一名优秀的程序员,十分优秀!