gpt4 book ai didi

html - 使用 XSL 显示 XML 节点但保持 XML 的顺序 - 具有多个命名空间

转载 作者:行者123 更新时间:2023-12-04 17:03:02 26 4
gpt4 key购买 nike

我有以下多命名空间 XML 文件:objects.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="objects.xsl"?>
<objects
xmlns:objA="http://www.mywebsite.com/objectA"
xmlns:objB="http://www.mywebsite.com/objectB"
>
<objA:objectA>
<objA:attrA1>Instance 1 Value 1</objA:attrA1>
<objA:attrA2>Instance 1 Value 2</objA:attrA2>
</objA:objectA>
<objB:objectB>
<objB:attrB1>Instance 2 Value 1</objB:attrB1>
<objB:attrB2>Instance 2 Value 2</objB:attrB2>
</objB:objectB>
<objA:objectA>
<objA:attrA1>Instance 3 Value 1</objA:attrA1>
<objA:attrA2>Instance 3 Value 2</objA:attrA2>
</objA:objectA>
<objB:objectB>
<objB:attrB1>Instance 4 Value 1</objB:attrB1>
<objB:attrB2>Instance 4 Value 2</objB:attrB2>
</objB:objectB>
</objects>

以及以下样式表 XLST 文件: objects.xsl
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:objA="http://www.mywebsite.com/objectA"
xmlns:objB="http://www.mywebsite.com/objectB"
>

<xsl:template match="/">
<html>
<body>

<xsl:for-each select="/objects/objA:objectA">
(<xsl:value-of select="objA:attrA1"/> |
<xsl:value-of select="objA:attrA2"/>)<br />
</xsl:for-each>

<xsl:for-each select="/objects/objB:objectB">
(<xsl:value-of select="objB:attrB1"/> |
<xsl:value-of select="objB:attrB2"/>)<br />
</xsl:for-each>

</body>
</html>
</xsl:template>

</xsl:stylesheet>

但结果并不完全是我想要的,因为使用上述文件我得到:
(Instance 1 Value 1 | Instance 1 Value 2)
(Instance 3 Value 1 | Instance 3 Value 2)
(Instance 2 Value 1 | Instance 2 Value 2)
(Instance 4 Value 1 | Instance 4 Value 2)

这与 XML 文件的顺序不同。我需要与 objects.xml 相同的订单: {1, 2, 3, 4}

我知道问题出在 for-each语句应用于不同的点,但我给出了上面的代码来显示我想要的近似值。

我尝试只使用一个 for-each对命名空间使用通配符的语句,但似乎命名空间不允许使用通配符。

任何的想法?

最佳答案

试试这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="/objects/*"/>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="concat('(', *[1],' | ', *[2],')')"/>
<br/>
</xsl:template>
</xsl:stylesheet>

关于html - 使用 XSL 显示 XML 节点但保持 XML 的顺序 - 具有多个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com