用XSLT替换f:facet属性

问题描述 投票:1回答:1

我试图通过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实现这一目标?我尝试了多个想法,但没有一个工作:(

谢谢 !

xslt xhtml
1个回答
0
投票

作为一个例子,我把你的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中可以使用定义的必需属性集并将其用于<app:formField>块,如下所示:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.