【问题标题】:Replace f:facet by property with XSLT将 f:facet 按属性替换为 XSLT
【发布时间】:2018-05-30 19:24:16
【问题描述】:

我正在尝试通过 XSLT 转换此 XML:

<app:formField type="display" styleClass="col-6">
    <f:facet name="label">
        <h:outputText value="#{messages['test']}"/>
    </f:facet>
    <h:inputText name="test"/>
</app:formField>

到这个输出:

<app:formField type="display" styleClass="col-6" label="#{messages['test']}">
    <h:inputText name="test"/>
</app:formField>

(添加标签属性并删除 f:facet name="label")

如何使用 XSLT 实现这一点?我尝试了多种想法,但都没有奏效:(

谢谢!

【问题讨论】:

  • 在这个问题中,jsf 确实起到了任何作用。只是 xhtml 作为 xml 而不是在 jsf 上下文中。请发布您尝试过的内容(SO 要求)

标签: xslt xhtml


【解决方案1】:

作为一个例子,我把你的 XML 部分如下:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="https://www.w3schools.com/furniture">
    <app:formField type="display" styleClass="col-6">
        <f:facet name="label">
            <h:outputText value="#{messages['test']}"/>
        </f:facet>
        <h:inputText name="test"/>
    </app:formField> 
</root>

然后可以在 XSL 中使用定义的所需属性集并将其用于&lt;app:formField&gt; 块,如下所示:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:app="http://schemas.android.com/apk/res-auto" 
                xmlns:h="http://www.w3.org/TR/html4/" 
                xmlns:f="https://www.w3schools.com/furniture"> 
    <xsl:output method="xml" />
    <!--create attribute structure as by requirements-->
    <xsl:attribute-set name="formField-attr-set">
        <xsl:attribute name="{name(/root/app:formField/@type)}">
            <xsl:value-of select="/root/app:formField/@type"/>
        </xsl:attribute>
        <xsl:attribute name="{name(/root/app:formField/@styleClass)}">
            <xsl:value-of select="/root/app:formField/@styleClass"/>
        </xsl:attribute> 
        <xsl:attribute name="{/root/app:formField/f:facet/@name}">
            <xsl:value-of select="/root/app:formField/f:facet/h:outputText/@value"/>
        </xsl:attribute>               
    </xsl:attribute-set>

    <xsl:template match="/root">
        <root>
            <!--apply already defined attribute set-->
            <xsl:element name="app:formField" use-attribute-sets="formField-attr-set">
               <xsl:copy-of select="/root/app:formField/h:inputText"/>
            </xsl:element>          
        </root>       
    </xsl:template>
</xsl:stylesheet>

结果如预期:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="https://www.w3schools.com/furniture" xmlns:app="http://schemas.android.com/apk/res-auto">
    <app:formField type="display" styleClass="col-6" label="#{messages['test']}">
        <h:inputText name="test"/>
    </app:formField>
</root>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2013-06-29
    • 2013-03-06
    • 2013-11-02
    • 2013-06-18
    相关资源
    最近更新 更多