Php 遍历XSLT中XML元素的所有属性
我试图使用Php 遍历XSLT中XML元素的所有属性,php,html,xml,xslt,Php,Html,Xml,Xslt,我试图使用获取给定元素的所有属性,但当我这样做时,我的语句不会执行 下面是我正在使用的元素: 下面是我正在使用的XSLT模板: <xsl:template match="textBox"> <div> <xsl:choose> <xsl:when test="@label"> <xsl:value-of select="@label"/> </xsl:when>
获取给定元素的所有属性,但当我这样做时,我的
语句不会执行
下面是我正在使用的元素:
下面是我正在使用的XSLT模板:<xsl:template match="textBox">
<div>
<xsl:choose>
<xsl:when test="@label">
<xsl:value-of select="@label"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>No Label Defined</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:element name="input">
<xsl:attribute name="type">text</xsl:attribute>
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="@id">
<xsl:attribute name="name">form_<xsl:value-of select="@id"/></xsl:attribute>
<xsl:attribute name="id">form_<xsl:value-of select="@id"/></xsl:attribute>
</xsl:when>
<xsl:when test="@label">
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</div>
未定义标签
文本
形式_
形式_
当我使用PHP生成HTML时,我得到了以下信息:<div>text 1<input type="text" id="Airfare" value="" label="text 1"></div>
text 1
如您所见,它没有将
表单
添加到id属性,它没有生成名称属性,也没有跳过标签属性。您好,您犯了两个错误:
<xsl:template match="textBox">
<div>
<xsl:choose>
<xsl:when test="@label">
<xsl:value-of select="@label"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>No Label Defined</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:element name="input">
<xsl:attribute name="type">text</xsl:attribute>
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name() = 'id'">
<xsl:attribute name="name">form_<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="id">form_<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:when test="@label">
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</div>
</xsl:template>
未定义标签
文本
形式_
形式_
我建议您更改其他测试条款的测试条件
这取决于您的xsl转换,但您可能希望在创建新的html属性时删除空白,例如:
<xsl:attribute name="id">
form_<xsl:value-of select="."/>
</xsl:attribute>
形式_
必须是:
<xsl:attribute name="name">form_<xsl:value-of select="."/>
表单_
当此转换应用于给定的XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vNoLabelVal" select="'No Label Defined'"/>
<xsl:template match="textBox">
<xsl:value-of select="concat(@label, substring($vNoLabelVal, 1 div not(@label)))"/>
<input type="text">
<xsl:apply-templates select="@*"/>
</input>
</xsl:template>
<xsl:template match="textBox/@*">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="textBox/@id">
<xsl:attribute name="name">
<xsl:value-of select="concat('form_', .)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="textBox/@label"/>
</xsl:stylesheet>
<textBox id="Airfare" value="" label="text 1"/>
text 1<input type="text" name="form_Airfare" value="" />
No Label Defined<input type="text" name="form_Airfare" value="" />
生成正确的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vNoLabelVal" select="'No Label Defined'"/>
<xsl:template match="textBox">
<xsl:value-of select="concat(@label, substring($vNoLabelVal, 1 div not(@label)))"/>
<input type="text">
<xsl:apply-templates select="@*"/>
</input>
</xsl:template>
<xsl:template match="textBox/@*">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="textBox/@id">
<xsl:attribute name="name">
<xsl:value-of select="concat('form_', .)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="textBox/@label"/>
</xsl:stylesheet>
<textBox id="Airfare" value="" label="text 1"/>
text 1<input type="text" name="form_Airfare" value="" />
No Label Defined<input type="text" name="form_Airfare" value="" />
未定义标签
好问题(+1)。请参阅我的答案,以了解问题的解释和一个符合XSLT精神的简短而简单的解决方案。