首页 > 解决方案 > 基于输入值的 xsl 转换

问题描述

我需要我的 xml 输出,但是输出 xml 是基于来自输入值的条件。输出 xml 不是输入 xml 的转换。xml 在另一个由文档函数访问的变量中。在这里,我使用文档功能在xslt本身中使用了 xml 。

文档函数访问的xml具有 id 属性,其值按升序排列。输入是一个数字(例如来自输入 xml),该数字应与从顶部选择元素开始访问的 xml 文档的 id 属性进行比较。如果在任何情况下对应的值(id 属性 + 15)大于输入数字,则应从输出xml中忽略特定的选择元素和前面的选择元素。

例如,如果输入数字为 100,则输出应如下所示,因为满足条件的第一个元素是 id 值“90”。因为 90 +15 大于 100。

<menu>
     <choice id="95">C</choice>
     <choice id="100">C</choice>
   </menu>

我的输入 xml 说

<a>100</a> 

我的 xslt 输出一切

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:elements>
   <menu>
     <choice id="20">ABC</choice>
     <choice id="23">B</choice>
     <choice id="30">C</choice>
     <choice id="37">C</choice>
     <choice id="45">C</choice>
     <choice id="50">C</choice>
     <choice id="90">C</choice>
     <choice id="95">C</choice>
     <choice id="100">C</choice>
   </menu>
 </my:elements>

 <xsl:template match="/">
  <xsl:copy-of select="document('')/*/my:elements/*"/>
 </xsl:template>
</xsl:stylesheet>

标签: xmlxslt-1.0

解决方案


这是一种方法。将其视为伪代码。您必须插入详细信息。

<!-- Find the id of the first choice that over the limit.  --> 
<xsl:variable name="firstChoiceOverLimitID" select="generate-id(document('')/*/my:elements//choice[number(@id) + 15 &gt; 100][1])"/> 
  
<xsl:template match="/">
  <!-- Output everything after the first choice over the limit.  -->
  <xsl:copy-of select="document('')/*/my:elements//choice[generate-id(.) = $firstChoiceOverLimitID]/following-sibling::choice"/>
</xsl:template>

推荐阅读