我有一个输入XML,如下所示
输入XML
<description-page>
<noted>12000 </noted>
<noted>15000</noted>
<noted>NOTE</noted>
</description-page>
<idescription-note>
<noted>12000</noted>
<noted>15000</noted>
<noted>ENG CHG</noted>
</idescription-note>
我希望输出为
<sample>
<input>
<noted>12000</noted>
<noted>12000</noted>
</input>
<input>
<noted>15000</noted>
<noted>15000</noted>
</input>
<input>
<noted>NOTE</noted>
<noted>ENG CHG</noted>
</input>
</sample>
所以这里每个描述页面(注明)需要idescription-note(注释)元素
我现在所做的是在xslt
<xsl-template match="description-page | idescription-note>
这就是我尝试使用xslt的方法,但我并没有在如何匹配两个节点方面遇到困难。
请在这里指导我。
此致 Karthic
答案 0 :(得分:0)
您可以使用position()
来同步description-page
和idescription-note
节点的相对位置,如下所示:
<xsl:template match="description-page">
<sample>
<xsl:for-each select="noted">
<input>
<xsl:variable name="position" select="position()" />
<noted>
<xsl:value-of select="//description-page/noted[$position]/text()"/>
</noted>
<noted>
<xsl:value-of select="//idescription-note/noted[$position]/text()"/>
</noted>
</input>
</xsl:for-each>
</sample>
</xsl:template>
编辑以下也可以实现这一目标而不是每次
<xsl:template match="description-page">
<sample>
<xsl:apply-templates select="noted"/>
</sample>
</xsl:template>
<xsl:template match="noted">
<input>
<xsl:variable name="position" select="position()" />
<noted>
<xsl:value-of select="./text()"/>
</noted>
<noted>
<xsl:value-of select="//idescription-note/noted[$position]/text()"/>
</noted>
</input>
</xsl:template>
<!--Suppress the description-page tree entirely-->
<xsl:template match="idescription-note">
</xsl:template>
我没有尝试使用大型xml,但是使用了样本
这么多吗?性能的一个方面是将//idescription-note
路径替换为此节点的实际完整路径。
答案 1 :(得分:0)
假设输入是
<root>
<description-page>
<noted>12000 </noted>
<noted>15000</noted>
<noted>NOTE</noted>
</description-page>
<idescription-note>
<noted>12000</noted>
<noted>15000</noted>
<noted>ENG CHG</noted>
</idescription-note>
</root>
然后是样式表
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="id-notes" select="//idescription-note/noted"/>
<xsl:template match="root">
<sample>
<xsl:apply-templates select="description-page/noted"/>
</sample>
</xsl:template>
<xsl:template match="description-page/noted">
<input>
<xsl:copy-of select="."/>
<xsl:variable name="pos" select="position()"/>
<xsl:copy-of select="$id-notes[$pos]"/>
</input>
</xsl:template>
</xsl:stylesheet>
输出
<sample>
<input>
<noted>12000 </noted>
<noted>12000</noted>
</input>
<input>
<noted>15000</noted>
<noted>15000</noted>
</input>
<input>
<noted>NOTE</noted>
<noted>ENG CHG</noted>
</input>
</sample>