我有类似的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个列表,首先使用具有id!="books.xml"的"dlentry“元素的值,然后使用id="books.xml".。然后,我应该比较这些列表,并与所有不匹配的元素一起给出注意信息。
就像“注意!!失踪!1984!失踪!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>
</..>我试过几百种组合,但不起作用。现在它应该加上“!失踪!”一个列表"id=books",但它只给了一个空的元素,你能指出我,请问我的错误在哪里?
发布于 2016-06-27 12:53:32
我假设您至少可以使用XSLT2.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())]"/>https://stackoverflow.com/questions/38052432
复制相似问题