每个节点每个位置的 XSL 唯一值
这变得更加复杂:)
现在我在最后一个问题中面临另一个问题,我们
现在设法仅从一个父节点获取唯一值:
<?xml version="1.0" encoding="ISO-8859-1"?>
<roots>
<root>
<name>first</name>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>C</something>
<something>P</something>
</item>
<item>
<something>A</something>
<something>L</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>D</something>
<something>A</something>
</item>
</root>
<root>
<name>second</name>
<item>
<something>E</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>F</something>
<something>A</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>H</something>
</item>
<item>
<something>D</something>
<something>G</something>
</item>
</root>
</roots>
现在我需要获取仅依赖于之前的一个节点但仅依赖于父节点上的元素的唯一值 第二个位置
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:key name="item-by-value" match="something"
use="concat(normalize-space(.), ' ', generate-id(./ancestor::root))"/>
<xsl:key name="rootkey" match="root" use="name"/>
<xsl:template match="/">
<xsl:for-each select="key('rootkey','first')">
<xsl:for-each select="item/something[1]">
<xsl:sort />
<xsl:if test="generate-id() = generate-id(key('item-by-value',
concat(normalize-space(.), ' ', generate-id(./ancestor::root))))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
<xsl:text>_________</xsl:text>
<xsl:for-each select="item/something[2]">
<xsl:sort />
<xsl:if test="generate-id() = generate-id(key('item-by-value',
concat(normalize-space(.), ' ', generate-id(./ancestor::root))))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这个 XSL 的 我得到 ABCD_________LP 我需要的结果是 ABCD_________ALP
有什么想法吗?
this get ever more complicated :)
now i face another issue in last question we managed to take unique values from only one parent node
now with:
<?xml version="1.0" encoding="ISO-8859-1"?>
<roots>
<root>
<name>first</name>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>C</something>
<something>P</something>
</item>
<item>
<something>A</something>
<something>L</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>D</something>
<something>A</something>
</item>
</root>
<root>
<name>second</name>
<item>
<something>E</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>A</something>
</item>
<item>
<something>F</something>
<something>A</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>A</something>
<something>A</something>
</item>
<item>
<something>B</something>
<something>H</something>
</item>
<item>
<something>D</something>
<something>G</something>
</item>
</root>
</roots>
now i need to get the unique values depending only from one node before but just from the elements on the second position
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:key name="item-by-value" match="something"
use="concat(normalize-space(.), ' ', generate-id(./ancestor::root))"/>
<xsl:key name="rootkey" match="root" use="name"/>
<xsl:template match="/">
<xsl:for-each select="key('rootkey','first')">
<xsl:for-each select="item/something[1]">
<xsl:sort />
<xsl:if test="generate-id() = generate-id(key('item-by-value',
concat(normalize-space(.), ' ', generate-id(./ancestor::root))))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
<xsl:text>_________</xsl:text>
<xsl:for-each select="item/something[2]">
<xsl:sort />
<xsl:if test="generate-id() = generate-id(key('item-by-value',
concat(normalize-space(.), ' ', generate-id(./ancestor::root))))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
with this XSL i get ABCD_________LP
where the result i need is ABCD_________ALP
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
再次,问题是,如果您想说“此根下具有此内容的第一个节点出现在
item
节点中的这个位置”,那么您必须将“在item
节点中的位置”添加到键中。您可以通过使用两个单独的键来实现此目的,如 Dimitre 的解决方案所做的那样,或者将键更改为:然后使两个测试表达式如下所示:
和:
Once again, the issue is that if you want to say "the first node with this content under this root appearing in this position in the
item
node", then you have to add "position in theitem
node" to the key. You can either do this by having two separate keys, as Dimitre's solution does, or change your key to:And then make your two test expressions look like:
and:
只需对我对您之前问题的回答稍加修改即可!
当此转换应用于提供的 XML 文档时,就会产生所需的正确结果 :
Just a slight modification to my answer to your previous question and you've got it!
When this transformation is applied on the provided XML document, The wanted, correct results are produced:
我知道您仅限于 XSLT 1.0 并且您正在使用 Xalan,但我将添加此答案,以防万一它可以帮助其他人将来进行搜索。 (那可以使用XSLT 2.0。)希望你不介意。
另外,我使用 Saxon-HE 9.2.0.6 作为处理器。
这是样式表:
这是使用 XML 的输出:
如果您想从输出中删除空格,您可以将输出放入变量中,然后使用
replace
删除空格。 (有人知道更好的方法吗?)样式表:
输出:
I know you're restricted to XSLT 1.0 and you're using Xalan, but I'm going to add this answer just in case it might help someone else doing a search in the future. (That can use XSLT 2.0.) Hope you don't mind.
Also, I'm using Saxon-HE 9.2.0.6 for the processor.
Here's the stylesheet:
Here's the output using your XML:
If you wanted to strip the spaces from the output, you could put the output in a variable and then use
replace
to strip the spaces. (Anyone know of a better way?)Stylesheet:
Output: