XSLT 2.0灵活,具有相同URI的默认或不同的前缀命名空间(XSLT 2.0 flexible with default or different prefixed namespace of the same URI)
我是XSLT的新手,并且在一些锅炉板命名空间处理上遇到了麻烦。
我有以下xslt,其目标是简单地重命名一个元素:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" > <xsl:strip-space elements="*" /> <!-- element template that copies over elements --> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> <!-- attribute template to copy attributes over --> <xsl:template match="@*"> <xsl:copy> <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute> </xsl:copy> </xsl:template> <!-- "other" template to copy the rest of the nodes --> <xsl:template match="comment() | text() | processing-instruction()"> <xsl:copy/> </xsl:template> <!-- Rename an element --> <xsl:template match="BOPPolicyQuoteInqRq/RqUID" > <xsl:element name="RqUUID"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet>
转换以下xml按预期工作:
<ACORD xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> <InsuranceSvcRq> <BOPPolicyQuoteInqRq> <RqUID>E2BA6308-62D5-43AC-B8C1-7616FDFE9C98</RqUID> </BOPPolicyQuoteInqRq> </InsuranceSvcRq> </ACORD>
但是,这个语义上等效的xml失败:
<bloat:ACORD xmlns:bloat="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> <bloat:InsuranceSvcRq> <bloat:BOPPolicyQuoteInqRq> <bloat:RqUID>E2BA6308-62D5-43AC-B8C1-7616FDFE9C98</bloat:RqUID> </bloat:BOPPolicyQuoteInqRq> </bloat:InsuranceSvcRq> </bloat:ACORD>
我收到的错误是:
Caused by: net.sf.saxon.trans.XPathException: Undeclared prefix in element name: bloat at net.sf.saxon.expr.instruct.ComputedElement.getElementName(ComputedElement.java:429) at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:388) at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:371) at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239) at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1056) at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:65) at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1044) at net.sf.saxon.Controller.transformDocument(Controller.java:2088) at net.sf.saxon.Controller.transform(Controller.java:1911) at org.apache.camel.builder.xml.XsltBuilder.process(XsltBuilder.java:141) at org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:103) at org.apache.camel.component.xslt.XsltEndpoint.onExchange(XsltEndpoint.java:121) at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.processor.Pipeline.process(Pipeline.java:121) at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380) at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270) at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380) at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:221) at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:124) at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:137) ... 32 more
即使这些xmls在语义上是等效的,只要涉及到xml规范,XSLT变换器就会挂起,因为一个声明了前缀而另一个没有声明(我还冒险说它会挂起,如果一个以'foo'为前缀,另一个以'bar'为前缀)。
我处于这样一个位置:我无法强制传递给我xml的客户端以某种方式声明特定的前缀或命名空间。 我也不能保证他们明天不会决定使用不同的前缀别名。
我对声明
xpath-default-namespace
属性的理解是它告诉xslt转换器整个文档将与哪个命名空间URI相关联,无论它是否被声明为默认前缀,别名为'bloat'的前缀,或者甚至是别名'rainbowunicorns'的前缀。
xpath-default-namespace
的属性究竟是做什么的,我怎样才能编写一个灵活的XSLT,它可以优雅地处理任意数量的语义等效命名空间,而不管客户端决定什么样的命名空间声明?规范如果相关:Camel 2.16.2 Saxon-HE 9.5.1-8
更新后的转换适用于xmls(由Martin Honnen提供):
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" > <xsl:strip-space elements="*" /> <!-- element template that copies over elements --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <!-- "other" template to copy the rest of the nodes --> <xsl:template match="comment() | processing-instruction()"> <xsl:copy/> </xsl:template> <!-- Rename an element --> <xsl:template match="BOPPolicyQuoteInqRq/RqUID" > <xsl:element name="RqUUID" namespace="{namespace-uri()}"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet>
I am new to XSLT and am stumbling on some boiler plate namespace handling.
I have the following xslt where the goal is to simply rename one element:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" > <xsl:strip-space elements="*" /> <!-- element template that copies over elements --> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> <!-- attribute template to copy attributes over --> <xsl:template match="@*"> <xsl:copy> <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute> </xsl:copy> </xsl:template> <!-- "other" template to copy the rest of the nodes --> <xsl:template match="comment() | text() | processing-instruction()"> <xsl:copy/> </xsl:template> <!-- Rename an element --> <xsl:template match="BOPPolicyQuoteInqRq/RqUID" > <xsl:element name="RqUUID"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet>
Transforming the following xml works as expected:
<ACORD xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> <InsuranceSvcRq> <BOPPolicyQuoteInqRq> <RqUID>E2BA6308-62D5-43AC-B8C1-7616FDFE9C98</RqUID> </BOPPolicyQuoteInqRq> </InsuranceSvcRq> </ACORD>
However, this semantically equivalent xml fails:
<bloat:ACORD xmlns:bloat="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> <bloat:InsuranceSvcRq> <bloat:BOPPolicyQuoteInqRq> <bloat:RqUID>E2BA6308-62D5-43AC-B8C1-7616FDFE9C98</bloat:RqUID> </bloat:BOPPolicyQuoteInqRq> </bloat:InsuranceSvcRq> </bloat:ACORD>
The error i receive is:
Caused by: net.sf.saxon.trans.XPathException: Undeclared prefix in element name: bloat at net.sf.saxon.expr.instruct.ComputedElement.getElementName(ComputedElement.java:429) at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:388) at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:371) at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239) at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1056) at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:65) at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1044) at net.sf.saxon.Controller.transformDocument(Controller.java:2088) at net.sf.saxon.Controller.transform(Controller.java:1911) at org.apache.camel.builder.xml.XsltBuilder.process(XsltBuilder.java:141) at org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:103) at org.apache.camel.component.xslt.XsltEndpoint.onExchange(XsltEndpoint.java:121) at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71) at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141) at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.processor.Pipeline.process(Pipeline.java:121) at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:412) at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:380) at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270) at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380) at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:221) at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:124) at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:137) ... 32 more
It appears even though these xmls are semantically equivalent as far as the xml spec would be concerned, the XSLT transformer is getting hung up because one declares a prefix and the other doesn't (i would also venture to say it will get hung up if one was prefixed with 'foo' and one with 'bar').
I am in a position where I can't force the client who's passing me xml to declare a specific prefix or namespace a certain way. I also can't guarantee they won't decide to utilize a different prefix alias tomorrow.
My understanding of declaring the
xpath-default-namespace
attribute was that it told the xslt transformer what namespace URI the entire document will be associated with regardless of whether it would be declared as a default prefix, a prefix with the alias 'bloat', or even a prefix with the alias 'rainbowunicorns'.What precisely does the attribute
xpath-default-namespace
do and how can I can write a flexible XSLT that can gracefully handle any amount of semantically equivalent namespaces regardless of what flavor of namespace declaration the client decides?Specs if relevant: Camel 2.16.2 Saxon-HE 9.5.1-8
Updated transform that works with both xmls (courtesy of Martin Honnen):
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" > <xsl:strip-space elements="*" /> <!-- element template that copies over elements --> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <!-- "other" template to copy the rest of the nodes --> <xsl:template match="comment() | processing-instruction()"> <xsl:copy/> </xsl:template> <!-- Rename an element --> <xsl:template match="BOPPolicyQuoteInqRq/RqUID" > <xsl:element name="RqUUID" namespace="{namespace-uri()}"> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:template> </xsl:stylesheet>
原文:https://stackoverflow.com/questions/38997742
最满意答案
数据列表只是一个自动完成列表,可用于文本输入元素。 如果你想预先设置一个值,只需设置输入的值:
<input list="cars" value="BMW" class="form-control" name="caBrands" style="width:300px;"> <datalist id="cars"> <option value="BMW"> <option value="Toyota"> <option value="Mitsubishi">
如果你想总是从列表中选择一个值,你可以使用select元素。 这允许将一个选项标记为选中状态,但不允许自由格式输入:
<select class="form-control" name="caBrands" style="width:300px;"> <option selected value="BMW">BMW</option> <option value="Toyota">Toyota</option> <option value="Mitsubishi">Mitsubishi</option> </select>
The datalist is just an auto-complete list to be used with a textual input element. If you want to pre-set a value, just set the value of the input:
<input list="cars" value="BMW" class="form-control" name="caBrands" style="width:300px;"> <datalist id="cars"> <option value="BMW"> <option value="Toyota"> <option value="Mitsubishi">
If you want to always select a value from a list, you can use a select element. This allows marking one option as selected, but it doesn't allow freeform input:
<select class="form-control" name="caBrands" style="width:300px;"> <option selected value="BMW">BMW</option> <option value="Toyota">Toyota</option> <option value="Mitsubishi">Mitsubishi</option> </select>
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您需要再次通过JSON进行解析才能获得lot 。 请参阅以下代码。 而且您不需要将lot设置为data属性。 var dataList = document.getElementById('lots'); var jsonOptions = [{ "machine": 1, "lot": "lot1" }, { "machine": 2, "lot": "lot2" }]; // Loop over the JSON array. jsonOptions.forEa ...
-
我会创建一个状态缩写数组并循环,检查状态是否匹配默认值,然后附加必要的HTML。 这是一个快速(功能)代码示例。 请注意,如果需要,可以将数组缩小为两个字母缩写:
-
数据列表只是一个自动完成列表,可用于文本输入元素。 如果你想预先设置一个值,只需设置输入的值:
-
价值需要实现这个词; 不是数字 这将允许输入文本的自动完成。 如果你需要退回一个号码; 我建议使用代码隐藏或javascript ...
-
需要datalist(Make datalist required)[2023-01-19]
如果你想进行服务器端验证,获取Datalist的数据属性(Get data attribute of Datalist)[2023-09-15]
您的代码中存在两个问题。 首先,您需要从input元素而不是datalist获取val() 。 其次, data属性名称应为小写,否则会干扰jQuery的内部缓存。 考虑到这一点,试试这个:通过删除结束标记来修改代码 检查这个小提琴演示 。 只需在文本框中聚焦时按下箭头键即可。 它将显示所有选项。 Modify your code by removing closing tag在DataList域中看起来有些倒退,具体取决于项目何时被渲染以及它所在的模板(查看下面的第一个链接以获取解释)。 ItemDataBound方法是有效的,但有时会像该文章中描述的那样有点古怪。 在你描述的情况下,我相信第二种方法是可行的,你可以在调用DataBind() 之前设置SelectedIndex属性。 步骤是: 提供PostBack是错误的 设置SelectedIndex 设置数据源 的DataBind 这是一个例子: void Page_Load(Object sender, EventA ...HTML 5 Datalist选项(HTML 5 Datalist Option)[2024-01-28]
我已经通过使用此代码解决了我的问题。 protected void Page_Load(object sender, EventArgs e) { bindProducts(); } private void bindProducts() { using (systemDbEntities context = new systemDbEntities()) { var q = from c in context.Products select new { c.Pro ...相关文章
更多- 快速了解XSLT
- There is no Action mapped for namespace / and action name register.
- OAuth 2.0 授权过程
- hadoop 2.0--YARN
- nutch2.0完全分布式部署配置
- 使用 XSLT 将 XML 转换为 XHTML
- Senparc.Weixin.MP SDK 微信公众平台开发教程(十二):OAuth2.0说明
- OAuth 2.0 授权、令牌、重定向端点
- OAuth 2.0 客户端如何获取授权
- struts 2.0
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。