我有像
这样的xml文件<section>
<dlentry xtrf="books.xml">
<dd>
<msgph>Harry Potter
</msgph>
</dd>
</dlentry>
<dlentry xtrf="books.xml">
<dd>
<msgph>1984
</msgph>
</dd>
</dlentry>
<dlentry xtrf="books.xml">
<dd>
<msgph>The Great Gatsby
</msgph>
</dt>
</dlentry>
<dlentry>
<dd>
<dl>
<dlentry xtrf="myFavouriteBooks.xml">
<dd>
<parml>
<plentry>
<pd>
<msgph>Harry Potter
</msgph>
</pd>
</plentry>
</parml>
</dd>
</dlentry>
</dl>
</dd>
</dlentry>
<dlentry xtrf="myBooks.xml">
<dd>
<msgph>1984
</msgph>
</dd>
</dlentry>
</section>
我需要做的是首先使用XSL创建2个列表 - 值为“ dlentry ”的元素具有 id!=“books.xml”第二个 - id =“books.xml”。在我应该比较theese列表并给出关注消息与所有不匹配的元素之后。
像“注意!!MISSING! 1984 !MISSING! The Great Gatsby ”
现在我有xsl:
<xsl:variable name="inBooks" select="/dlentry/dd/msgph"/>
<xsl:variable name="notInBooks" select="//dlentry[not(contains(@xtrf,'books.xml))]//msgph/node()[not(self::dlentry)])" as="item()*"/>
<title>Books</title>
<refbody>
<section>
<dl>
<dlentry>
<xsl:variable name="notMatched" select="//dlentry[not(contains(@xtrf,'books.xml'))]//msgph[msgph !='$inBooks']/node()[not(self::dlentry)])"/>
<xsl:choose>
<xsl:when test="$notMatched">
<xsl:for-each select="section/dl/dlentry">
<dt>
<xsl:value-of select="concat('!MISSING! ', msgph")/>
</dt>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="section/dl/dlentry">
<dt>
<xsl:value-of select="msgph"/>
</dt>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<dt>
<xsl:value-of select="$notInBooks"/>
</dt>
<../>
它给出了输出:
<title>Books</title>
<refbody>
<section>
<dl>
<dlentry>
<dt>The Great Gatsby</dt>
<dt>1984</dt>
<dt>Harry Potter Part 2</dt>
<dt>Harry Potter Part 1</dt>
<dd/>
<dt>Harry Potter Part 1 Harry Potter Part 2</dt>
</..>
我已经尝试了数百种组合,但它不起作用..现在它应该添加“!MISSING!”到列表“id = books”,但它只给出一个空元素 你能指出我,我的错误在哪里,拜托?
答案 0 :(得分:0)
我假设您至少可以使用XSLT 2.0,那么您可以使用
<xsl:variable name="b1" select="//dlentry[contains(@xtrf, 'books.xml')]"/>
<xsl:variable name="b2" select="//dlentry[not(contains(@xtrf, 'books.xml'))]"/>
<xsl:variable name="b3" select="$b1[not(.//msgph/normalize-space() = $b2//msgph/normalize-space())]"/>