xsl转换问题
下一个问题:我有“InputDoc”xml 文档和xslt 文件,以便转换为其他“OutputDoc”xml 文档。 您可以在下面找到示例 xslt 和 xml 文档。
<?xml version="1.0" encoding="UTF-8"?>
<InputDoc>
<InputCollection>
<InputItem>
<InputValue>Value_1</InputValue>
</InputItem>
</InputCollection>
</InputDoc>
<?xml version="1.0" encoding="utf-16"?>
<OutputDoc>
<OutputElement>
<OutputItem>
<OutputValue>Value_1</OutputValue>
</OutputItem>
<OutputDescription>Description_1</OutputDescription>
</OutputElement>
</OutputDoc>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//InputCollection">
<OutputDoc>
<xsl:for-each select="InputItem">
<OutputElement>
<OutputItem>
<OutputValue><xsl:value-of select="InputValue" /></OutputValue>
</OutputItem>
<OutputDescription>
<xsl:call-template name="InputValue2OutputDescriptionMappings">
<xsl:with-param name="InputValueParam" select="InputValue" />
</xsl:call-template>
</OutputDescription>
</OutputElement>
</xsl:for-each>
</OutputDoc>
</xsl:template>
<xsl:template name="InputValue2OutputDescriptionMappings">
<xsl:param name="InputValueParam" />
<xsl:choose>
<xsl:when test="$InputValueParam='Value_1'">Description_1</xsl:when>
<xsl:when test="$InputValueParam='Value_2'">Description_2</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
有用。但是,如果输出 xml 文档仅包含“OutputElement”节点(其中包含“InputValue2OutputDescriptionMappings”中的值),即如果“OutputDescription”节点的值为空,则“OutputElement”节点将不会包含在“OutputDoc”中,那就太好了。
我如何使用上面的 XSL 转换来做到这一点?
Next question: I have "InputDoc" xml document and xslt file in order to transform into other "OutputDoc" xml document.
You can find examples xslt and xml documents below.
<?xml version="1.0" encoding="UTF-8"?>
<InputDoc>
<InputCollection>
<InputItem>
<InputValue>Value_1</InputValue>
</InputItem>
</InputCollection>
</InputDoc>
<?xml version="1.0" encoding="utf-16"?>
<OutputDoc>
<OutputElement>
<OutputItem>
<OutputValue>Value_1</OutputValue>
</OutputItem>
<OutputDescription>Description_1</OutputDescription>
</OutputElement>
</OutputDoc>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//InputCollection">
<OutputDoc>
<xsl:for-each select="InputItem">
<OutputElement>
<OutputItem>
<OutputValue><xsl:value-of select="InputValue" /></OutputValue>
</OutputItem>
<OutputDescription>
<xsl:call-template name="InputValue2OutputDescriptionMappings">
<xsl:with-param name="InputValueParam" select="InputValue" />
</xsl:call-template>
</OutputDescription>
</OutputElement>
</xsl:for-each>
</OutputDoc>
</xsl:template>
<xsl:template name="InputValue2OutputDescriptionMappings">
<xsl:param name="InputValueParam" />
<xsl:choose>
<xsl:when test="$InputValueParam='Value_1'">Description_1</xsl:when>
<xsl:when test="$InputValueParam='Value_2'">Description_2</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It works. But It would be nice if output xml document contained just "OutputElement" nodes which contains values into "InputValue2OutputDescriptionMappings", i.e. if value of "OutputDescription" node is empty then "OutputElement" node won't be included into "OutputDoc".
How I can do it using XSL transformation above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用
key()
优化的标准 XSLT 1.0 样式表:结果:
注意:“模式匹配”。
key()
具有多个文档(本例为样式表)。编辑:由于不匹配的值请求而错过了
OutputElement
,抱歉。现在,输入:
输出:
注意:这都是模板驱动的。因此,您甚至可以对
description
内容应用更多逻辑。This is an standard XSLT 1.0 stylesheet optimized with
key()
:Result:
Note: "Pattern matching".
key()
with multiple documents (this case, the stylesheet).Edit: Miss strip
OutputElement
for non matching value request, sorry.Now, this input:
Output:
Note: This is all template driven. So, you can apply more logic even to
description
content.您必须首先获取映射的结果,然后测试它是否为空并省略
标记,如下所示:但是,这里是一个存储映射的更像 XSL 风格的版本在变量中省去映射模板,并使用“推”处理(应用模板)而不是“拉”处理。
You have to get the result of the mapping first, THEN test it for empty and omit the
<OutputDescription>
tag, as in:However, here is a much more XSL-esque version that stores the mappings in a variable to dispense with the mapping template, and uses "push" processing (apply-templates) instead of "pull" processing.