如何使用xslt从XML读取注释并在元素中添加注释文本
我正在使用XSLT和XML 我随身带着下面的XML如何使用xslt从XML读取注释并在元素中添加注释文本,xml,xslt,Xml,Xslt,我正在使用XSLT和XML 我随身带着下面的XML <?xml version="1.0" encoding="UTF-8"?> <mappings> <!-- News mapping --> <mapping old="mbp" new="/SessionHandler.aspx?pageurl=/BP.aspx&pub=/english&section=IBE&j=f"/>
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<!-- News mapping -->
<mapping old="mbp" new="/SessionHandler.aspx?pageurl=/BP.aspx&pub=/english&section=IBE&j=f"/>
<mapping old="about/news" new="about/news/news.aspx"/>
<!-- CUGO's-->
<mapping old="/nhs" new="/cugo.aspx?promoCode=UKNHS01&pub=/uk/english"/>
<mapping old="/hk/ukstudentfare" new="/cugo.aspx?promoCode=HKSTU10&pub=/hk/Chinese"/>
<!-- Reserved below vanity URL's -->
<mapping old="/kgfmastercard" new=""/>
<mapping old="/mastercard" new=""/>
<!-- Other vanity URL's-->
<mapping old="/destinationbriefs" new="http://www.ekgroup.com/destinationbriefs"/>
<mapping old="/win" new="/ch/german/destinations_offers/win_two_tickets_on_A380.aspx"/>
<!--FIFA Fan Fest-->
<mapping old="/romefanfest" new="/it/italian/destinations_offers/rome_international_fifa_fan_fest.aspx"/>
<mapping old="/parisfanfest" new="/fr/french/destinations_offers/paris_international_fifa_fan_fest.aspx"/>
</mappings>
下面是我正在使用的xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="xsl fo">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:for-each select="mappings/mapping">
<VanityUrl>
<old>
<xsl:value-of select="@old" />
</old>
<new>
<xsl:value-of select="@new" />
</new>
<dateAdded>2010-05-03T14:45:00</dateAdded>
<xsl:if test="@new = ''">
<NotLive>Yes</NotLive>
</xsl:if>
<xsl:if test="preceding-sibling::comment()">
<comments>
<xsl:value-of select="preceding-sibling::comment()" />
</comments>
</xsl:if>
</VanityUrl>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
2010-05-03T14:45:00
对
现在我试图得到如下结果:
<VanityUrl>
<old>/mbp</old>
<new>/SessionHandler.aspx?pageurl=/BP.aspx&pub=/english&section=IBE&j=f</new>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>News mapping</comments>
</VanityUrl>
<VanityUrl>
<old>about/news</old>
<new>about/news/news.aspx</new>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>News mapping</comments>
</VanityUrl>
<VanityUrl>
<old>/nhs</old>
<new>/cugo.aspx?promoCode=UKNHS01&pub=/uk/english</new>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>CUGO's</comments>
</VanityUrl>
<VanityUrl>
<old>/hk/ukstudentfare</old>
<new>/cugo.aspx?promoCode=HKSTU10&pub=/hk/Chinese</new>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>CUGO's</comments>
</VanityUrl>
<VanityUrl>
<old>/kgfmastercard</old>
<new></new>
<NotLive>yes</NotLive>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>Reserved below vanity URL's</comments>
</VanityUrl>
<VanityUrl>
<old>/mastercard</old>
<new></new>
<NotLive>yes</NotLive>
<dateAdded>2010-05-03T14:45:00 </dateAdded>
<comment>Reserved below vanity URL's</comments>
</VanityUrl>
/mbp
/SessionHandler.aspx?pageurl=/BP.aspx&;pub=/英语和英语;截面=IBE&;j=f
2010-05-03T14:45:00
新闻地图
关于/新闻
关于/news/news.aspx
2010-05-03T14:45:00
新闻地图
/国民保健制度
/cugo.aspx?promoCode=UKNHS01&;pub=/uk/英语
2010-05-03T14:45:00
库戈
/香港/英国学生票价
/cugo.aspx?promoCode=HKSTU10&;pub=/hk/Chinese
2010-05-03T14:45:00
库戈
/kgfmastercard
对
2010-05-03T14:45:00
保留在URL的下面
/万事达卡
对
2010-05-03T14:45:00
保留在URL的下面
一切正常,但我无法处理评论部分,如何根据这些相关评论添加评论元素。我的意思是,我的xslt中的以下代码工作不正常,它只添加了第一条注释,即新闻映射
xsl:if test="preceding-sibling::comment()">
<comments>
<xsl:value-of select="preceding-sibling::comment()" />
</comments>
</xsl:if>
xsl:if test=“preference sibling::comment()”>
前面的同级::comment()
以与文档相反的顺序提供当前级别上所有前面注释节点的节点集。但是,您需要选择最接近的注释以使其生效:
<xsl:if test="preceding-sibling::comment()[1]">
<comments>
<xsl:value-of select="preceding-sibling::comment()[1]" />
</comments>
</xsl:if>