gpt4 book ai didi

使用 XSLT 1 进行 XML 转换

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:55 27 4
gpt4 key购买 nike

今天我真的在为 XSLT 苦苦挣扎,我不得不使用它已经有很长时间了。我必须编辑一些xml,我不能使用 XSLT 2.0。所以我必须使用 1.0.我正在努力的 xml 是(基本示例):

我尝试为这两个节点制作一个模板,然后“调用”该模板以创建一个具有所需值的新节点,但这也没有用,如果有人能指出正确的方向,我会遗漏一些东西。

<messagemap>
<author>
<au_id>274-80-9391</au_id>
<au_lname>Straight</au_lname>
<au_fname>Dean</au_fname>
<phone>415 834-2919</phone>
<address>5420 College Av.</address>
<city>Oakland</city>
<state>CA</state>
<zip>94609</zip>
<contract>1</contract>
</author>
</messagemap>

XM:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!--Identity Transform.-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="au_fname | au_lname">
<company>
<xsl:value-of select="."/>
</company>
</xsl:template>
</xsl:stylesheet>

我得到的结果:

<messagemap>
<author>
<au_id>274-80-9391</au_id>
<company>Straight</company>
<company>Dean</company>
<phone>415 834-2919</phone>
<address>5420 College Av.</address>
<city>Oakland</city>
<state>CA</state>
<zip>94609</zip>
<contract>1</contract>
</author>
</messagemap>

我需要的是:

<messagemap>
<author>
<au_id>274-80-9391</au_id>
<company>Dean Straight</company>
<phone>415 834-2919</phone>
<address>5420 College Av.</address>
<city>Oakland</city>
<state>CA</state>
<zip>94609</zip>
<contract>1</contract>
</author>
</messagemap>

最佳答案

您可以尝试匹配 au_fname 并构建 company。然后您可以剥离 au_lname

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<!--Identity Transform.-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="au_fname">
<company>
<xsl:value-of select="normalize-space(concat(.,' ',../au_lname))"/>
</company>
</xsl:template>

<xsl:template match="au_lname"/>

</xsl:stylesheet>

关于使用 XSLT 1 进行 XML 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911527/

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