gpt4 book ai didi

xslt - 在xslt中选择一个并行节点

转载 作者:行者123 更新时间:2023-12-03 17:36:39 25 4
gpt4 key购买 nike

考虑以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<base>
<a>
<b>
<c>Text 1</c>
</b>
</a>
</base>
<base>
<a>
<b>
<c>Text 2</c>
</b>
</a>
</base>
</root>


和这个xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>

<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:value-of select="text()" />
<!-- i need Text 2 here -->
</xsl:if>
<xsl:apply-templates />
</xsl:template>

<xsl:template match="text()" />
</xsl:stylesheet>


原始xml嵌套得多,我不知道确切的结构。但是,存在一个具有相同结构的并行节点。如果我的模板位于// root / base [1] / a / b / c,我想引用// root / base [2] / a / b / c

但是我只知道我在// root / base [1]下的某个节点中,并且在// root / base [2]中有相同的节点。

是否有可能实现这一目标?

最佳答案

此样式表不需要扩展名,并且不对唯一名称进行任何假设;无聊的部分是每次调用xsl:apply-templates时都会传递正确的“双节点”:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:apply-templates select="root/base[1]">
<xsl:with-param name="twin" select="root/base[2]"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="*">
<xsl:param name="twin"/>
<xsl:if test="text()">
<xsl:value-of select="text()" />
<xsl:value-of select="$twin/text()"/>
</xsl:if>
<xsl:for-each select="*">
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select=".">
<xsl:with-param name="twin" select="$twin/*[$pos]"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>

<xsl:template match="text()" />
</xsl:stylesheet>

关于xslt - 在xslt中选择一个并行节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193533/

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