我需要一些标签的一部分。我的XML就是这样的
<div class="item">
<h2><a href="url.html" title="siomethink">Vyzivovy poradca</a></h2>
...
...
<div class="watch"><a href="sth" data-id="292931" data-active="somethink" data-inactive="blablalba" data-class="monitored" class="watchItem" title="watching"><span>sometihink</span></a></div>
</div>我需要href属性和"data-id“属性。我的模板看起来
<xsl:variable name="url" select="xhtml:h2/xhtml:a/href"/>
<xsl:variable name="job_id" select="xhtml:div[@class = 'watch']/xhtml:a/data-id"/>
<job>
<xsl:attribute name="id"><xsl:value-of select="$job_id"/></xsl:attribute>
<url name="url"><xsl:value-of select="$url"/></url>
</job>
标记a的模板是:
<xsl:template match="xhtml:a">
<xsl:copy>
<!-- can not copy href, cause it is not absolute url ! -->
<xsl:copy-of select="@align|@title|@rel|@itemprop|@itemtype|@itemscope"/>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:apply-templates select="*|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()"><xsl:value-of select="normalize-space(.)"/></xsl:template>
<xsl:template match="text()[ancestor::xhtml:pre]"><xsl:value-of select="etl:regex-replace(., '(\s|\n)+', '$1', 'g')"/></xsl:template>但这不管用,有什么想法吗?
发布于 2013-11-19 19:50:07
此输入XML:
<div class="item">
<h2>
<a href="url.html" title="siomethink">Vyzivovy poradca</a>
</h2>
...
...
<div class="watch">
<a href="sth"
data-id="292931"
data-active="somethink"
data-inactive="blablalba"
data-class="monitored"
class="watchItem"
title="watching"><span>sometihink</span></a>
</div>
</div>提供给XSLT的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="//a[descendant::text() = 'sometihink']">
<root>
<href>
<xsl:value-of select="@href"/>
</href>
<data-id>
<xsl:value-of select="@data-id"/>
</data-id>
</root>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>生成这个输出XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<href>sth</href>
<data-id>292931</data-id>
</root>备注:
sometihink“内容是您所寻求的a最独特的特性。如果是其他的东西(比如父div[@class="watch"]),让我知道,我们可以调整。以下是OP的最新评论:
更新了 XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<root>
<item-href>
<xsl:value-of select="//div[@class='item']/h2/a/@href"/>
</item-href>
<watch-data-id>
<xsl:value-of select="//div[@class='watch']/a/@data-id"/>
</watch-data-id>
</root>
</xsl:template>
</xsl:stylesheet>给定上述输入XML,将产生以下输出XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item-href>url.html</item-href>
<watch-data-id>292931</watch-data-id>
</root>包含请求的属性值。
https://stackoverflow.com/questions/20080240
复制相似问题