gpt4 book ai didi

XSLT:将文本添加到 XML 中的自关闭标签

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

我正在尝试将文本添加到空/自关闭标签。

我想将诸如“ ”之类的内容转换为“ some text ”。

这是我正在处理的 xml 的缩短版本:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<attr tag="00080090" vr="PN" pos="-1" name="Referring Physician's Name" vm="0" len="0"/>
</dataset>

我想得到这个结果:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<attr tag="00080090" vr="PN" pos="-1" name="Referring Physician's Name" vm="0" len="0">this is the inserted text</attr>
</dataset>

但相反,我最终得到了一个未修改的 xml。如果此标签不存在文本,则我的匹配似乎不起作用。如果已经有一些文本,它会起作用,在这种情况下它会替换文本,这对我来说很好。

我的 XSL(T) 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="no"/>

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

<xsl:template match="attr[@tag='00080090']/text()">
<xsl:text>this is the inserted text</xsl:text>
</xsl:template>

</xsl:stylesheet>

我用 http://xslttest.appspot.com 做了我的测试

任何提示?

最佳答案

XSLT 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="no"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attr[@tag='00080090']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:text>this is the inserted text</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

关于XSLT:将文本添加到 XML 中的自关闭标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882068/

24 4 0