首页 \ 问答 \ XSLT 2.0灵活,具有相同URI的默认或不同的前缀命名空间(XSLT 2.0 flexible with default or different prefixed namespace of the same URI)

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
更新时间:2021-10-11 16:10

最满意答案

数据列表只是一个自动完成列表,可用于文本输入元素。 如果你想预先设置一个值,只需设置输入的值:

 <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>

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。