我有以下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 & 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]//*
提前感谢您的回答
答案 0 :(得分:3)
<强>予。 XPath 2.0解决方案:
使用强>:
/*/article/*[. >> ../head and ../portal >> .]
<强> II。 XPath 1.0解决方案(它也是一个XPath 2.0解决方案):
/*/article/head/following-sibling::*[following-sibling::portal]
<强> III。基于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 & 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 & 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表达式(上图)并将所选节点复制到输出,产生完全相同的结果(上图)。
答案 1 :(得分:0)
如果第一级只有一个头部和门户节点,则可以枚举头部之后和门户节点之前的所有节点,然后带走他们的子节点:
//head/following::*[following::portal]/descendant-or-self::*
或者如果有多篇文章:
//article/head/following-sibling::*[following-sibling::portal]/descendant-or-self::*