简单的 XSLT 问题:设置内部 HTML 节点属性值,例如
给定设计的 XML 模式、示例 XML 输入和下面用于将 XML 转换为 HTML 的示例 XSLT。如何设置标签内的属性?例如
、
等?注意:HouseNumber 和 Zipcode 周围缺少引号是故意的。我试图将这些属性的值从 XML 输入放入 id=""、for=""、name="" 等。
示例 XML 架构
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Location">
<xs:complexType>
<xs:attribute name="State" type="xs:string" use="required" />
<xs:attribute name="County" type="xs:string" use="required" />
<xs:attribute name="City" type="xs:string" use="required" />
<xs:attribute name="Zipcode" type="xs:nonNegativeInteger" use="required" />
<xs:attribute name="HouseNumber" type="xs:nonNegativeInteger" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
示例 XML 输入:< /strong>
<Location>
<State>California</State>
<County>Los Angeles County</County>
<City>Los Angeles</City>
<Zipcode>90210</Zipcode>
<HouseNumber>123</HouseNumber>
</Location>
示例 XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="Location">
<!--Inner HTML example, div has no id-->
<div class="houseStyle">
<ul>
<li><xsl:value-of select="Location/State"/></li>
<li><xsl:value-of select="Location/County"/></li>
<li><xsl:value-of select="Location/City"/></li>
<li><xsl:value-of select="Location/Zipcode"/></li>
</ul>
</div>
<!--Inner HTML example again, but how do I
set the div id to HouseNumber?-->
<div class="houseStyle" id=HouseNumber>
<ul>
<li><xsl:value-of select="Location/State"/></li>
<li><xsl:value-of select="Location/County"/></li>
<li><xsl:value-of select="Location/City"/></li>
<li><xsl:value-of select="Location/Zipcode"/></li>
</ul>
</div>
</xsl:for-each>
</xsl:stylesheet>
所需的 HTML 输出,其中 div 标记具有门牌号码的 id:
<div class="houseStyle" id="123">
<ul>
<li>California</li>
<li>Los Angeles County</li>
<li>Los Angeles</li>
<li>90210</li>
</ul>
</div>
Given the contrived XML schema, sample XML input, and sample XSLT below used to transform XML to HTML. How do I set attributes within tags? For example <div id=HouseNumber>
,<input type="checkbox" id=Zipcode>
, etc?
Note: The lack of quotes around HouseNumber and Zipcode are on purpose. I am trying to put the value of these attributes from the XML input into id="", for="", name="", etc.
Sample XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Location">
<xs:complexType>
<xs:attribute name="State" type="xs:string" use="required" />
<xs:attribute name="County" type="xs:string" use="required" />
<xs:attribute name="City" type="xs:string" use="required" />
<xs:attribute name="Zipcode" type="xs:nonNegativeInteger" use="required" />
<xs:attribute name="HouseNumber" type="xs:nonNegativeInteger" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Sample XML Input:
<Location>
<State>California</State>
<County>Los Angeles County</County>
<City>Los Angeles</City>
<Zipcode>90210</Zipcode>
<HouseNumber>123</HouseNumber>
</Location>
Sample XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="Location">
<!--Inner HTML example, div has no id-->
<div class="houseStyle">
<ul>
<li><xsl:value-of select="Location/State"/></li>
<li><xsl:value-of select="Location/County"/></li>
<li><xsl:value-of select="Location/City"/></li>
<li><xsl:value-of select="Location/Zipcode"/></li>
</ul>
</div>
<!--Inner HTML example again, but how do I
set the div id to HouseNumber?-->
<div class="houseStyle" id=HouseNumber>
<ul>
<li><xsl:value-of select="Location/State"/></li>
<li><xsl:value-of select="Location/County"/></li>
<li><xsl:value-of select="Location/City"/></li>
<li><xsl:value-of select="Location/Zipcode"/></li>
</ul>
</div>
</xsl:for-each>
</xsl:stylesheet>
Desired HTML Output, where the div tag has an id of a house number:
<div class="houseStyle" id="123">
<ul>
<li>California</li>
<li>Los Angeles County</li>
<li>Los Angeles</li>
<li>90210</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
目前尚不清楚你想要什么。您的意思是要将属性设置为某些 XPath 表达式(例如
Delta
)的结果吗?如果是这样,这应该可以解决问题:或者,您可以使用
和
,正如其他答案所描述的那样,尽管典型用途这种情况是必须生成元素/属性名称本身的情况。It is not clear what you want here. Do you mean that you want to set your attribute to the result of some XPath expression (like
Delta
)? If so, this should do the trick:Alternatively you may use
<xsl:element>
and<xsl:attribute>
, as other answers describe, though the typical use case for that is when element/attribute name itself has to be generated.尝试:
锚标记示例:
Try:
Example for an anchor tag:
下面的代码怎么样?
华泰
What about code below?
HTH