gpt4 book ai didi

php - 在 xslt 转换中保留节点

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

<root>
<tag>
<form>
some html form will be here
</form>

</tag>
<tag>
some visible data
</tag>

xslt
<xsl:template match="tag">
<div id="page-base">
<xsl:apply-templates />
</div>
</xsl:template>

生产
<div id="page-base">

</div>
<div id="page-base">
some visible data
</div>

所需输出
<div id="page-base">
<form>
some html form will be here
</form>
</div>
<div id="page-base">
some visible data
</div>

编辑:

如果 tag怎么办嵌套在 tag应用模板规则的元素将用模板替换标签,并复制模板不匹配的其他元素。请看例子。 tag可以任意嵌套
<root>
<tag>
<form>
some html form will be here
</form>
<tag>
arbitrary nested tags
</tag>
</tag>
<tag>
some visible data
</tag>
</root>

预期结果
 <div id="page-base">
<form>
some html form will be here
</form>
<div id="page-base">
arbitrary nested tags
</div>
</div>
<div id="page-base">
some visible data
</div>

最佳答案

本次改造 :

<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="tag">
<div id="page-base">
<xsl:copy-of select="node()"/>
</div>
</xsl:template>
</xsl:stylesheet>

当应用于提供的 XML 文档时:
<root>
<tag>
<form>
some html form will be here
</form>
</tag>
<tag>
some visible data
</tag>
</root>

产生想要的正确结果:
<div id="page-base">
<form>
some html form will be here
</form>
</div>
<div id="page-base">
some visible data
</div>

说明 :

您的代码缺少复制匹配 tag 正文的内容元素。

这个“东西”是 xsl:copy-of 指令。

更新 :

OP 改变了他的问题,这需要不同的解决方案:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/*"><xsl:apply-templates/></xsl:template>

<xsl:template match="tag">
<div id="page-base">
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>

当此转换应用于新提供的 XML 文档时:
<div id="page-base">
<form>
some html form will be here
</form>
<div id="page-base">
arbitrary nested tags
</div>
</div>
<div id="page-base">
some visible data
</div>

关于php - 在 xslt 转换中保留节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408009/

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