在XSL中使用for-each创建新的子节点



我找到了许多关于xsl:for-each的教程、文章、帖子等,但似乎没有一个符合我的用例,所以如果这个问题已经在其他地方得到了回答,我表示歉意。

我有以下示例XML(有多个文档项):

<doc>
<att>
<PRef>52545125</PRef>
<BlobContent Name="rtf" ID="A7A9348763A111EA887800505685156C">
<Property Name="MimeType" Value="application/rtf"/>
<Property Name="FileName" Value="P0001A__m.rtf"/>
<Reference Link="Directoryexport_819280402327681192_P0001A__m.rtf"
Type="RELATIVEFILE"/>
</BlobContent>
<InvoiceDate>01/01/1970</InvoiceDate>
<NetAmount>100.75</NetAmount>
<GrossAmount/>
<BlobContent Name="tif" ID="A7A90D7663A111EA887800505685156C">
<Property Name="MimeType" Value="image/tiff"/>
<Property Name="FileName" Value="P0001A__m.tif"/>
<Reference Link="Directoryexport_4747308861722121077_P0001A__m.tif"
Type="RELATIVEFILE"/>
</BlobContent>
</att>

我试图从每个BlobContent

更改以下内容
<Reference Link="Directoryexport_4747308861722121077_P0001A__m.tif" Type="RELATIVEFILE"/>

读作:

<rtf>Directoryexport_819280402327681192_P0001A__m.rtf</rtf>
<tif>Directoryexport_4747308861722121077_P0001A__m.tif</tif>

下面的xsl将按预期工作,但将数据放入标签中,不需要定义任何内容,这是tif或rtf:

<?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="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/doc/att/BlobContent/Reference">
<xsl:variable name="element-name" select="@Link"/>
<xsl:element name="Link">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

我试着添加一个for-each来创建基于BlobContent名称的新标签,但得到一个错误,说明{tif rtf}是一个无效的QName,所以它似乎正在拾取每个值,但将它们分组成一个元素名称。

感谢

使用模板

<xsl:template match="/doc/att/BlobContent/Reference">
<xsl:element name="{../@Name}">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:template>

最新更新