题
<result>
<binding name="PropertyURI">
<uri>http://dbpedia.org/ontology/motto</uri>
</binding>
<binding name="Property">
<literal xml:lang="en">motto</literal>
</binding>
<binding name="ValueURI">
<uri>http://dbpedia.org/ontology/motto</uri>
</binding>
<binding name="Value">
<literal>Ittehad, Tanzim, Yaqeen-e-Muhkam(Urdu)</literal>
</binding>
</result>
我想变换它像
<a href=PropertyURI>Property</a>
<a href=ValueURI>Value</a>
问题是结合标签的该数量可以不同。有时候,我们可能只的URI或ONY值。
我怎么能知道XSLT,如果用@名绑定= PropertyURI可用?点击 如果是的话那么接下来是什么约束力@name属性的名称?
解决方案
有已经似乎有效的答案,但我刚刚花了10分钟测试下面的代码,以便:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="/result/binding[@name='PropertyURI']"/>
</xsl:template>
<xsl:template match="binding">
<a>
<xsl:attribute name="href">
<xsl:value-of select="./uri"/>
</xsl:attribute>
<xsl:value-of select="./following-sibling::binding[1][@name='Property']/literal"/>
</a>
</xsl:template>
</xsl:stylesheet>
其他提示
这不是很清楚你在找什么,因为你似乎对问题的描述和所需输出不被相关。我的认为的,你要做的就是找到一个binding
孩子每uri
元素的东西,发现有一个binding
孩子相关literal
元素,并使用uri
和literal
值来填充a
元素。
此模板假定因为在一个与binding
孩子name
属性与所述一个与所述uri
子,例如所述name
属性开始两个literal
元件相关“PropertyURI”开始于“属性”和“ValueURI”开始于“值”:
<xsl:template match="binding[uri]">
<xsl:variable name="name" value="@name"/>
<xsl:variable name="literal"
select="/result/binding[starts-with($name, @name)]/literal"/>
<xsl:if test="$literal">
<a href="{uri}">
<xsl:value-of select="$literal"/>
</a>
</xsl:if>
</xsl:template>
如果相关元件仅仅是下 binding
元件的一个与所述uri
子后,可使用上述模板,具有替换变量赋值:
<xsl:variable name="literal" select="following-sibling::binding[1]/literal"/>
不隶属于 StackOverflow