我创建了一个XSLT,我想知道如何在一组标签之间复制所有节点,并在底部添加另一个标签.我创建了XSLT,它具有确定要添加哪个标记以及应该调用的标记的所有逻辑.但是我现在遇到的问题是我无法复制所有其他标签.以下是有问题的文件:
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="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/csvImportSchema">
<csvImportSchema>
<xsl:for-each select="payload">
<payload>
<xsl:copy-of select="@*"/>
<xsl:variable name="ean">
<xsl:value-of select="ean"/>
</xsl:variable>
<xsl:for-each select="../product">
<xsl:if test="ean = $ean">
<productId><xsl:value-of select="article"/></productId>
</xsl:if>
</xsl:for-each>
</payload>
</xsl:for-each>
</csvImportSchema>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
INPUT
<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
<payload>
<test>1</test>
<test2>2</test2>
<test3>3</test3>
<ean>1111111111</ean>
<productId/>
</payload>
<product>
<article>722619</article>
<ean>1111111111</ean>
</product>
</csvImportSchema>
Run Code Online (Sandbox Code Playgroud)
当前输出
<?xml version="1.0" encoding="utf-8"?>
<csvImportSchema>
<payload>
<productId>722619</productId>
</payload>
</csvImportSchema>
Run Code Online (Sandbox Code Playgroud)
期望的输出
<?xml version="1.0" encoding="UTF-8"?>
<csvImportSchema>
<payload>
<test>1</test>
<test2>2</test2>
<test3>3</test3>
<ean>1111111111</ean>
<productId>722619</productId>
</payload>
</csvImportSchema>
Run Code Online (Sandbox Code Playgroud)
这个简短而简单的转变:
<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="productId">
<productId>
<xsl:value-of select="../../product/article"/>
</productId>
</xsl:template>
<xsl:template match="product"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的XML文档时:
<csvImportSchema>
<payload>
<test>1</test>
<test2>2</test2>
<test3>3</test3>
<ean>1111111111</ean>
<productId/>
</payload>
<product>
<article>722619</article>
<ean>1111111111</ean>
</product>
</csvImportSchema>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<csvImportSchema>
<payload>
<test>1</test>
<test2>2</test2>
<test3>3</test3>
<ean>1111111111</ean>
<productId>722619</productId>
</payload>
</csvImportSchema>
Run Code Online (Sandbox Code Playgroud)
说明:
的身份规则的拷贝"原样"的每个节点为它被选择用于执行.
匹配的重写模板product从输出中"删除"此元素(通过其空体).
另一个重写模板匹配productId并生成带有文本节点子元素的元素product/article.