【发布时间】:2018-07-05 23:23:46
【问题描述】:
假设我有一个这样的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<everything>
<something>
<part>
<prop type="a">1</prop>
<prop type="b">2</prop>
<prop type="c">3</prop>
</part>
<part>
<prop type="a">4</prop>
<!--No b!-->
<prop type="c">5</prop>
</part>
</something>
<something>
<part>
<prop type="a">6</prop>
<prop type="b">7</prop>
<!--No c!-->
</part>
</something>
</everything>
我想要这样的输出:
<something>
<props type="a">14</props>
<props type="b">2?</props>
<props type="c">35</props>
</something>
<something>
<props type="a">6</props>
<props type="b">7</props>
<props type="c">?</props>
</something>
也就是说,首先我想知道文档中所有的prop/@types 是什么。在这种情况下,“a”、“b”和“c”。我已经找到了该部分的解决方案,我将对此问题进行掩饰。接下来,对于每个“某物”,我想遍历那些预期的道具元素,并将当前“某物”元素中该类型的所有道具合并到一个名为props 的新元素下。如果在 part 元素中找不到预期的 prop 元素之一,则插入问号。
这是我最好的尝试,如下。我认为它至少存在两个问题。一方面,在<xsl:for-each select="$allTheProps/prop">̀ 循环的范围内,我不能像通常在$allTheProps 循环之外那样做select="part"。另一个问题是我认为你不能在路径模式中有一个变量,所以我无法测试@type=$nodeType。最终结果是我的 props 元素只是空的。我该如何解决这些问题?
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:variable name="allTheProps">
<!--These elements are actually dynamically generated
based on all prop types found in the document-->
<prop type="a"></prop>
<prop type="b"></prop>
<prop type="c"></prop>
</xsl:variable>
<xsl:template match="something">
<xsl:copy>
<!--for each found type-->
<xsl:for-each select="$allTheProps/prop">
<xsl:variable name="nodeType" select="@type"/>
<props>
<xsl:attribute name="type"><xsl:value-of select="$nodeType" />
</xsl:attribute>
<!--merge the props from each part of a something node-->
<xsl:for-each select="part">
<xsl:value-of select="prop[@type=$nodeType]"/>
<xsl:if test="not(prop[@type=$nodeType])">?</xsl:if>
</xsl:for-each>
</props>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:transform>
【问题讨论】:
-
您还没有明确说明您需要 XSLT 1.0 还是 2.0 解决方案。请使用适当的标签。
标签: xml variables xpath foreach xslt-1.0