我面临的问题对其他人来说可能很简单,但作为初学者的 XSL - 我还没有找到合适的解决方案。我想要做的是通过连接在 for-each 循环中定义的变量的结果来构建一个字符串。
抱歉应该是这样的:
<linked-hash-map>
<entry>
<string>properties</string>
<list>
<linked-hash-map>
<entry>
<string>property_id</string>
<int>123</int>
</entry>
<entry>
<string>type</string>
<string>H</string>
</entry>
<entry>
<string>status</string>
<string>CURRENT</string>
</entry>
<entry>
<string>description</string>
<string>Test</string>
</entry>
<entry>
<string>lots</string>
<list>
<linked-hash-map>
<entry>
<string>lot_id</string>
<int>123</int>
</entry>
<entry>
<string>lot_number</string>
<int>11</int>
</entry>
<entry>
<string>plan_number</string>
<int>100</int>
</entry>
<entry>
<string>plan_type</string>
<string>CC</string>
</entry>
<entry>
<string>plan_id</string>
<int>1</int>
</entry>
</linked-hash-map>
</list>
</entry>
</linked-hash-map>
</list>
</entry>
所需输出:SP - 31 - 108661
所以基本上我想将plan_type、lot_number和plan_number连接成一个字符串。
我试过这个:
<xsl:for-each select="linked-hash-map/entry/linked-hash-map/entry/list/linked-hash-map/entry" >
<xsl:choose>
<!-- LOT NUMBER -->
<xsl:when test="string[1] = 'lot_number'">
<xsl:variable name="lot_number" select="int" />
</xsl:when>
<!-- PLAN NUMBER -->
<xsl:when test="string[1] = 'plan_number'">
<xsl:variable name="plan_number" select="int" />
</xsl:when>
<!-- PLAN TYPE -->
<xsl:when test="string[1] = 'plan_type'">
<xsl:variable name="plan_type" select="string[2]" />
</xsl:when>
</xsl:choose>
</xsl:for-each>
<!-- LOT DETAIL -->
// concatenate string in here.
<xsl:value-of select="concat($plan_type, '-', $lot_number, '-', $plan_number)" />
但它不起作用,因为变量超出范围。我怎样才能实现所需的输出?谢谢你。
要尽可能少地更改,请尝试以下 XSLT-1.0 代码:
<xsl:template match="linked-hash-map">
<xsl:variable name="plan_type" select="entry[string='plan_type']/string[2]/text()" />
<xsl:variable name="lot_number" select="entry[string='lot_number']/int/text()" />
<xsl:variable name="plan_number" select="entry[string='plan_number']/int/text()" />
<xsl:value-of select="concat($plan_type, ' - ', $lot_number, ' - ', $plan_number)" />
</xsl:template>
它的结果是:
SP - 31 - 108661
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句