1

我有以下 xml 文档(仅显示结构的摘要,因为它很长):

<document>    
<article>
            <head>ISA Savers Could Lose Tax Relief</head>
            <text>
                <paragraph>Tax relief could be withdrawn from tens of thousands of Individual Savings Account, sheltering up to £500 million from tax, because some banks and building societies are flouting not only the spirit, but the letter of the laws governing them.</paragraph>
                <paragraph>This will come as alarming news to thousands of savers who invested in fixed rate Isas which they thought would offer them security combined with a tax shelter The Inland Revenue is writing to all Britain's savings institutions reminding them of the basic Isa rules and warning that tax relief will be withdrawn if these are manipulated in anyway.</paragraph>
                <paragraph>Furthermore, the Revenue will castigate some deposit-takers for already breaching the rules and demand they speedily rewrite their terms and conditions, or face loss of tax-relief.</paragraph>
                <paragraph>The guardian of the nations coffers is known to be deeply disturbed at the rapid recent growth of fixed- rate Isas which lock savers in for several years, maintaining these are unlawful. It is sending a stark reminder to all savings institutions that customers must be allowed to withdraw their cash, and requiring those which don't, to address the violation.</paragraph>
                <paragraph>At the front of the firing line are Bradford &amp; Bingley, Liverpool Victoria, and Julian Hodge Bank, which attracted huge inflows of money because they have been consistently among the best buys. All three specifically prohibit withdrawals. The Ipswich Building Society joined them last week, becoming the latest to offer a fixed- rate Isa, outlawing withdrawals until the end of the term.</paragraph>
                <paragraph>A written statement from the Revenue said:"When ISAs were introduced Ministers made it clear that one of the their main objectives was to encourage non-savers to start saving, and people with small amounts saved, to save more. It was therefore important that ISAs would not lock in savers' money as this would exclude people with limited resources who might need access to their money quickly.</paragraph>
                <paragraph>"We do not consider that a product whose terms and conditions actually prevented withdrawals or transfers during a fixed term would be consistent with Ministers' intentions or the statutory rules."</paragraph>
                <paragraph>So serious are the Revenue's concerns that they no longer trust banks and building societies to monitor themselves. They plan to introduce a new requirement which insists institutions submit full details of all Isa accounts to them for approval, before they becomes available to the public. Until now, banks merely had to satisify themselves they were not breaking the rules.</paragraph>
                <paragraph>Paul James, marketing manager of Julian Hodge Bank, said the fixed accounts were launched in response to customer demand.</paragraph>
                <paragraph>He said:"People were asking for a fixed rate, and those who have taken them out over the past couple of years have done well. We understood that Isas had to be transferable on notice, which we took to be the end of the fixed period.</paragraph>
                <paragraph>"We were audited by the Revenue last year, and the issue was not raised at that stage, so we thought everything was OK. If we have been successfully audited then the accounts can't be otherwise but OK surely?"</paragraph>
                <paragraph>Nigel Snell, a spokesman for Liverpool Victoria said that its range of five Isas prohibiting withdrawals did include a proviso in the small print allowing access to funds "in exceptional circumstances and by permission of the trustees".</paragraph>
                <paragraph>He added:"We make it clear to people that their money is locked away, but thought the account was compliant because of this clause in the small print."</paragraph>
                <paragraph>An Ipswich spokesman said it was aware there was some ambiguity when its account was launched last week. However, it acknowledged it may have to review its terms and conditions again, after seeing the letter from the Revenue.</paragraph>
            </text>
            <date>
                <day>10</day>
                <month>05</month>
                <year>2002</year>
            </date>
            <source>Sexymoney</source>
            <portal>Finance</portal>
            <ID number="2610981009.98103"/>
        </article>
.
.
.

我必须找到每篇文章中出现在头节点之后和门户节点之前的所有节点。

我不知道如何遍历文章节点的所有子节点和孙节点,并将其与同一文章节点的头节点和门户节点进行比较。

这是我解决 xpath 查询的尝试之一(这显然不起作用,因为我不知道如何遍历所有节点进行比较):

for $x in //article return $x[$x/text >> $x/head and $x/text << $x/portal]//*

提前感谢您的回答

4

2 回答 2

3

一、XPath 2.0 解决方案

使用

/*/article/*[. >> ../head and ../portal >> .]

二、XPath 1.0 解决方案(它也是一个 XPath 2.0 解决方案):

/*/article/head/following-sibling::*[following-sibling::portal]

三、基于 XSLT 的验证

这个 XSLT 2.0 转换

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:sequence select="/*/article/*[. >> ../head and ../portal >> .]"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<document>
    <article>
        <head>ISA Savers Could Lose Tax Relief</head>
        <text>
            <paragraph>Tax relief could be withdrawn from tens of thousands of Individual Savings Account, sheltering up to £500 million from tax, because some banks and building societies are flouting not only the spirit, but the letter of the laws governing them.</paragraph>
            <paragraph>This will come as alarming news to thousands of savers who invested in fixed rate Isas which they thought would offer them security combined with a tax shelter The Inland Revenue is writing to all Britain's savings institutions reminding them of the basic Isa rules and warning that tax relief will be withdrawn if these are manipulated in anyway.</paragraph>
            <paragraph>Furthermore, the Revenue will castigate some deposit-takers for already breaching the rules and demand they speedily rewrite their terms and conditions, or face loss of tax-relief.</paragraph>
            <paragraph>The guardian of the nations coffers is known to be deeply disturbed at the rapid recent growth of fixed- rate Isas which lock savers in for several years, maintaining these are unlawful. It is sending a stark reminder to all savings institutions that customers must be allowed to withdraw their cash, and requiring those which don't, to address the violation.</paragraph>
            <paragraph>At the front of the firing line are Bradford &amp; Bingley, Liverpool Victoria, and Julian Hodge Bank, which attracted huge inflows of money because they have been consistently among the best buys. All three specifically prohibit withdrawals. The Ipswich Building Society joined them last week, becoming the latest to offer a fixed- rate Isa, outlawing withdrawals until the end of the term.</paragraph>
            <paragraph>A written statement from the Revenue said:"When ISAs were introduced Ministers made it clear that one of the their main objectives was to encourage non-savers to start saving, and people with small amounts saved, to save more. It was therefore important that ISAs would not lock in savers' money as this would exclude people with limited resources who might need access to their money quickly.</paragraph>
            <paragraph>"We do not consider that a product whose terms and conditions actually prevented withdrawals or transfers during a fixed term would be consistent with Ministers' intentions or the statutory rules."</paragraph>
            <paragraph>So serious are the Revenue's concerns that they no longer trust banks and building societies to monitor themselves. They plan to introduce a new requirement which insists institutions submit full details of all Isa accounts to them for approval, before they becomes available to the public. Until now, banks merely had to satisify themselves they were not breaking the rules.</paragraph>
            <paragraph>Paul James, marketing manager of Julian Hodge Bank, said the fixed accounts were launched in response to customer demand.</paragraph>
            <paragraph>He said:"People were asking for a fixed rate, and those who have taken them out over the past couple of years have done well. We understood that Isas had to be transferable on notice, which we took to be the end of the fixed period.</paragraph>
            <paragraph>"We were audited by the Revenue last year, and the issue was not raised at that stage, so we thought everything was OK. If we have been successfully audited then the accounts can't be otherwise but OK surely?"</paragraph>
            <paragraph>Nigel Snell, a spokesman for Liverpool Victoria said that its range of five Isas prohibiting withdrawals did include a proviso in the small print allowing access to funds "in exceptional circumstances and by permission of the trustees".</paragraph>
            <paragraph>He added:"We make it clear to people that their money is locked away, but thought the account was compliant because of this clause in the small print."</paragraph>
            <paragraph>An Ipswich spokesman said it was aware there was some ambiguity when its account was launched last week. However, it acknowledged it may have to review its terms and conditions again, after seeing the letter from the Revenue.</paragraph>
        </text>
        <date>
            <day>10</day>
            <month>05</month>
            <year>2002</year>
        </date>
        <source>Sexymoney</source>
        <portal>Finance</portal>
        <ID number="2610981009.98103"/>
    </article>
</document>

计算 XPath 2.0 表达式并将所有选定节点复制到输出

<text>
            <paragraph>Tax relief could be withdrawn from tens of thousands of Individual Savings Account, sheltering up to £500 million from tax, because some banks and building societies are flouting not only the spirit, but the letter of the laws governing them.</paragraph>
            <paragraph>This will come as alarming news to thousands of savers who invested in fixed rate Isas which they thought would offer them security combined with a tax shelter The Inland Revenue is writing to all Britain's savings institutions reminding them of the basic Isa rules and warning that tax relief will be withdrawn if these are manipulated in anyway.</paragraph>
            <paragraph>Furthermore, the Revenue will castigate some deposit-takers for already breaching the rules and demand they speedily rewrite their terms and conditions, or face loss of tax-relief.</paragraph>
            <paragraph>The guardian of the nations coffers is known to be deeply disturbed at the rapid recent growth of fixed- rate Isas which lock savers in for several years, maintaining these are unlawful. It is sending a stark reminder to all savings institutions that customers must be allowed to withdraw their cash, and requiring those which don't, to address the violation.</paragraph>
            <paragraph>At the front of the firing line are Bradford &amp; Bingley, Liverpool Victoria, and Julian Hodge Bank, which attracted huge inflows of money because they have been consistently among the best buys. All three specifically prohibit withdrawals. The Ipswich Building Society joined them last week, becoming the latest to offer a fixed- rate Isa, outlawing withdrawals until the end of the term.</paragraph>
            <paragraph>A written statement from the Revenue said:"When ISAs were introduced Ministers made it clear that one of the their main objectives was to encourage non-savers to start saving, and people with small amounts saved, to save more. It was therefore important that ISAs would not lock in savers' money as this would exclude people with limited resources who might need access to their money quickly.</paragraph>
            <paragraph>"We do not consider that a product whose terms and conditions actually prevented withdrawals or transfers during a fixed term would be consistent with Ministers' intentions or the statutory rules."</paragraph>
            <paragraph>So serious are the Revenue's concerns that they no longer trust banks and building societies to monitor themselves. They plan to introduce a new requirement which insists institutions submit full details of all Isa accounts to them for approval, before they becomes available to the public. Until now, banks merely had to satisify themselves they were not breaking the rules.</paragraph>
            <paragraph>Paul James, marketing manager of Julian Hodge Bank, said the fixed accounts were launched in response to customer demand.</paragraph>
            <paragraph>He said:"People were asking for a fixed rate, and those who have taken them out over the past couple of years have done well. We understood that Isas had to be transferable on notice, which we took to be the end of the fixed period.</paragraph>
            <paragraph>"We were audited by the Revenue last year, and the issue was not raised at that stage, so we thought everything was OK. If we have been successfully audited then the accounts can't be otherwise but OK surely?"</paragraph>
            <paragraph>Nigel Snell, a spokesman for Liverpool Victoria said that its range of five Isas prohibiting withdrawals did include a proviso in the small print allowing access to funds "in exceptional circumstances and by permission of the trustees".</paragraph>
            <paragraph>He added:"We make it clear to people that their money is locked away, but thought the account was compliant because of this clause in the small print."</paragraph>
            <paragraph>An Ipswich spokesman said it was aware there was some ambiguity when its account was launched last week. However, it acknowledged it may have to review its terms and conditions again, after seeing the letter from the Revenue.</paragraph>
        </text>
<date>
            <day>10</day>
            <month>05</month>
            <year>2002</year>
        </date>
<source>Sexymoney</source>

这个 XSLT 1.0 转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/article/head/following-sibling::*[following-sibling::portal]"/>
 </xsl:template>
</xsl:stylesheet>

在提供的 XML 文档(上图)上计算 XPath 1.0 表达式并将所选节点复制到输出,产生完全相同的结果(上图)

于 2012-11-07T13:34:48.600 回答
0

如果第一层只有一个头和门户节点,可以枚举一个头之后和门户节点之前的所有节点,然后取它们的子节点:

//head/following::*[following::portal]/descendant-or-self::*

或者如果有多篇文章:

//article/head/following-sibling::*[following-sibling::portal]/descendant-or-self::*
于 2012-11-07T13:31:29.567 回答