如何在 source xml 命名空间内生成元素 (xslt 1.0)
how to generate elements inside source xml namespace (xslt 1.0)
我有一个简单的 xml 我正在尝试处理,例如
<?xml version="1.0" encoding="utf-8"?>
<root>
<foo>1234</foo>
</root>
使用 xslt...我想将数据注入到此 xml 中,然后将其处理成 excel...所以像这样可怕的东西...在 psuedo xslt 1.0 中。 .但与相关作品
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:kooks ="http://kookerella.com" exclude-result-prefixes="kooks msxsl">
<xsl:template match="/root">
<xsl:variable name="bar">
<xsl:choose>
<xsl:when test="...">
<xsl:copy-of select="foo"/>
</xsl:when>
<xsl:otherwise>
<foo>5678</foo>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:foreach select="msxml:node-set($bar)/foo">
...do some stuff...
</xsl:foreach>
</xsl:template>
</xsl:stylesheet>
现在的问题是我在 xslt 中定义的“foo”元素在不同的命名空间中(我认为),因此它在“foreach”处理部分中不匹配...那么我该如何强制执行此操作xslt 元素位于正确的命名空间中?
您的样式表声明了一个默认命名空间:
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
不确定为什么需要这样做。当它在那里时,样式表创建的任何元素都将继承此名称空间 - 具体来说 foo
in:
<xsl:otherwise>
<foo>5678</foo>
</xsl:otherwise>
您可以通过将创建的元素显式放置在无命名空间中来防止这种情况发生:
<xsl:otherwise>
<foo xmlns="">5678</foo>
</xsl:otherwise>
但可能有更好的方法来实现它应该实现的任何目的。
P.S。您的标题说 “如何在源 xml 命名空间内生成元素” - 但源 XML 没有命名空间。如果要将复制的元素放在默认命名空间中,则必须在目标命名空间中重新创建它,而不是复制它。 Copy的意思是复制——包括原来的命名空间。
我有一个简单的 xml 我正在尝试处理,例如
<?xml version="1.0" encoding="utf-8"?>
<root>
<foo>1234</foo>
</root>
使用 xslt...我想将数据注入到此 xml 中,然后将其处理成 excel...所以像这样可怕的东西...在 psuedo xslt 1.0 中。 .但与相关作品
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:kooks ="http://kookerella.com" exclude-result-prefixes="kooks msxsl">
<xsl:template match="/root">
<xsl:variable name="bar">
<xsl:choose>
<xsl:when test="...">
<xsl:copy-of select="foo"/>
</xsl:when>
<xsl:otherwise>
<foo>5678</foo>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:foreach select="msxml:node-set($bar)/foo">
...do some stuff...
</xsl:foreach>
</xsl:template>
</xsl:stylesheet>
现在的问题是我在 xslt 中定义的“foo”元素在不同的命名空间中(我认为),因此它在“foreach”处理部分中不匹配...那么我该如何强制执行此操作xslt 元素位于正确的命名空间中?
您的样式表声明了一个默认命名空间:
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
不确定为什么需要这样做。当它在那里时,样式表创建的任何元素都将继承此名称空间 - 具体来说 foo
in:
<xsl:otherwise>
<foo>5678</foo>
</xsl:otherwise>
您可以通过将创建的元素显式放置在无命名空间中来防止这种情况发生:
<xsl:otherwise>
<foo xmlns="">5678</foo>
</xsl:otherwise>
但可能有更好的方法来实现它应该实现的任何目的。
P.S。您的标题说 “如何在源 xml 命名空间内生成元素” - 但源 XML 没有命名空间。如果要将复制的元素放在默认命名空间中,则必须在目标命名空间中重新创建它,而不是复制它。 Copy的意思是复制——包括原来的命名空间。