gpt4 book ai didi

javascript - 如何使用 xsl :copy-of to copy text of XML node and pass to form? 改用 CDATA?

转载 作者:行者123 更新时间:2023-11-29 19:45:33 26 4
gpt4 key购买 nike

我正在尝试使用 <xsl:copy-of>提交两个 XML 节点的副本以及一个表单。问题是我只获取节点的文本内容而不是所有完整的节点内容。根据w3schools :

The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well!

但是,这似乎并没有发生在我身上——要么我不理解注释,要么我正在使用 <xsl:copy-of>元素不正确。我在下面包含了我的 xml、xsl 和 jQuery 的片段(我将 xsl 应用到 xml,它创建了一个 html 表单,它使用 jQuery 提交)。我还包括了我目前获得的输出以及所需的输出。非常感谢任何建议!

XML 看起来像这样:

<trigger offsetBeg="89" offsetEnd="96">induced</trigger>
<ggps>
<ggp role="Theme" offsetBeg="68" offsetEnd="73" id="10013"
consensusName="HDAC6">HDAC6</ggp>
<ggp role="Cause" offsetBeg="100" offsetEnd="103" id="7001"
consensusName="TSA">TSA</ggp>
</ggps>

XSL 看起来像这样:

<xsl:variable name="trigger"><xsl:copy-of select="trigger" /></xsl:variable>
<xsl:variable name="ggps"><xsl:copy-of select="ggps" /></xsl:variable>
<form name="label_form" action="">
<input class="trigger" type="hidden" name="trigger" value="{$trigger}" />
<input class="ggps" type="hidden" name="ggps" value="{$ggps}" />
<div><button class="button">Submit</button></div>
</form>

当我将其添加到 XSL 时:

<xsl:copy-of select="current()/trigger"></xsl:copy-of>
<xsl:copy-of select="current()/ggps"></xsl:copy-of>

我得到这个 HTML:

<trigger offsetBeg="89" offsetEnd="96">induced</trigger>
<ggps>
<ggp role="Theme" offsetBeg="68" offsetEnd="73" id="10013"
consensusName="HDAC6">HDAC6</ggp>
<ggp role="Cause" offsetBeg="100" offsetEnd="103" id="7001"
consensusName="TSA">TSA</ggp>
</ggps>

我正在使用 jQuery 来提交表单:

$(".button").click(function() {
var trigger = $(this).parent().siblings("input.trigger").val();
var ggps = $(this).parent().siblings("input.ggps").val();
var dataString = 'trigger=' + trigger + '&ggps=' + ggps;

$.ajax({
type : "POST",
url : "/submit_label",
data : dataString,
success : function(){
console.log("dataString:\n" + dataString);
}
});
return false;
});

我从 console.log 得到的是这样的:

dataString:
trigger='induced '&ggps=' HDAC6 TSA '

我想从 console.log 得到的是这样的:

dataString:
trigger='<trigger offsetBeg="89" offsetEnd="96">induced</trigger>'&ggps='<ggps>
<ggp role="Theme" offsetBeg="68" offsetEnd="73"id="10013"consensusName="HDAC6">
HDAC6</ggp><ggp role="Cause" offsetBeg="100" offsetEnd="103" id="7001"
consensusName="TSA">TSA</ggp></ggps>'

我想要这个,因为我希望能够将这些节点直接放入我将要构建的另一个 XML 文档中。

最佳答案

您的变量 $trigger 包含相关树的完整副本,但是当您使用这样的属性值模板时:

value="{$trigger}"

该值被扁平化为一个字符串 - 具体来说,根元素的字符串值,它是其后代文本节点的串联。

如果您希望属性包含树的序列化版本(即,包含标记的词法 XML,可能会转义以使其成为合法的属性值),那么您需要调用某种 serialize-xml() 函数. XSLT 3.0 和一些产品(例如 Saxon)中有这样的功能;或者您可以自己编写。

关于javascript - 如何使用 xsl :copy-of to copy text of XML node and pass to form? 改用 CDATA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20024026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com