如何使用 XSLT 2.0 根据字符串中的整数检索节点?
这是我的 XML 文档:
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para1</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para2</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para3</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading4" />
</w:ppr>
<w:r>
<w:t>Para4</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para5</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para6</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading4" />
</w:ppr>
<w:r>
<w:t>Para7</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para8</w:t>
</w:r>
</w:p>
<!-- This is my Current Node -->
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para9</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para10</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
因此,在读取每个元素时,我想检查 w:ppr/w:pstyle
的属性 w:val
。例如,在上面的文件中,第一个 w:p
包含 (w:ppr/w:pstyle/@w:val
) 属性值作为 Heading1
。所以,对于第一个w:p
,我什么都不关心,只是接受它。之后,采用此 w:p
:
- 我想拆分属性值
Heading1
以检索标题后的字符串。所以,现在我们得到1
。然后,在读取下一个w:p
时,应用相同的逻辑来拆分当前属性值。因此,在本例中,我们有2
。现在,我想将当前值2
与之前的值1
进行比较。 - 如果它小于先前的值,则仅选择当前的
w:p
。否则,不要做任何事情。
对所有 w:p
节点应用上述步骤 1 和 2。因此,就我而言,我只想选择以下 w:p
节点。
示例输出节点为(完全获取整个 w:p 节点):
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para1</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para5</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para8</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para10</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
This is my XML document:
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para1</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para2</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para3</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading4" />
</w:ppr>
<w:r>
<w:t>Para4</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para5</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para6</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading4" />
</w:ppr>
<w:r>
<w:t>Para7</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para8</w:t>
</w:r>
</w:p>
<!-- This is my Current Node -->
<w:p>
<w:ppr>
<w:pstyle w:val="Heading3" />
</w:ppr>
<w:r>
<w:t>Para9</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para10</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
So, while reading each element, I want to check the attribute w:val
of w:ppr/w:pstyle
. For example, in the above file, the first w:p
contains that (w:ppr/w:pstyle/@w:val
) attribute value as Heading1
. So, For the first w:p
, i don't care about anything and just take it. After, taken this w:p
:
- I want to split the attribute value
Heading1
for retrieving strings after the Heading. So, Now we get1
. Afterwards, while reading the nextw:p
, apply the same logic for splitting the current attribute value. So, in this case, we have2
. Now, I want to compare this current value2
with previous value1
. - If it is less than the previous value then only select the current
w:p
. Otherwise, don't do anything.
Apply the above step 1 and 2 for all of the w:p
nodes. So, In my case, I want to select, only the following w:p
nodes.
Sample output nodes are (getting whole w:p node completely):
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para1</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para5</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading2" />
</w:ppr>
<w:r>
<w:t>Para8</w:t>
</w:r>
</w:p>
<w:p>
<w:ppr>
<w:pstyle w:val="Heading1" />
</w:ppr>
<w:r>
<w:t>Para10</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的意思是,当相关的 w:val 属性大于 w:val 时,您希望忽略 w:p 节点> 前一个 w:p 元素(如果存在)。
在 XPath 术语中,其中...
这是完整的 XSLT,它是恒等变换,具有额外的情况来匹配此类 w:p 节点并忽略它们:
当应用于输入样本时,生成以下输出:
I believe what you are saying is that you wish to ignore w:p nodes when the relevant w:val attribute is greater than the w:val element of the previous w:p (where it exists).
In XPath terms, where...
Here is the full XSLT, which is the identity transform, with the extra case to match such w:p nodes and ignore thems:
When applied to your input sample, the following output is generated: