【问题标题】:XSLT conditions matching with list stored in external fileXSLT 条件与存储在外部文件中的列表匹配
【发布时间】:2011-12-21 15:23:45
【问题描述】:

我正在尝试编写一个 XSLT,它提取的项目与另一个文件中列出的条件匹配。

输入文件(input.xml)

<ItemList>
   <Item>
      <Product>ABC</Product>
      <Price>10.00</Price>
   </Item>
   <Item>
      <Product>DEF</Product>
      <Price>20.00</Price>
   </Item>
   <Item>
      <Product>GHI</Product>
      <Price>30.00</Price>
   </Item>
   <Item>
      <Product>JKL</Product>
      <Price>40.00</Price>
   </Item>
</ItemList>

外部列表文件(Codes.xml)

<ProductCodeList>
   <ProductCode>ABC</ProductCode>
   <ProductCode>JKL</ProductCode>
</ProductCodeList>

预期输出 (output.xml)

<ItemList>
   <Item>
      <Product>ABC</Product>
      <Price>10.00</Price>
   </Item>
   <Item>
      <Product>JKL</Product>
      <Price>40.00</Price>
   </Item>
</ItemList>

你能告诉我哪个不工作吗?

<xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />`

<xsl:template match="/">
  <xsl:apply-templates select="/ItemList/Item[Product=$productCodeList]"/>
</xsl:template>

<xsl:template match="/ItemList/Item">
  <xsl:copy-of select="."/>
</xsl:template>

【问题讨论】:

    标签: list xslt variables


    【解决方案1】:

    这个简单的(无条件,无current())转换:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="vProds" select=
      "document('file:///c:/temp/delete/ProductList.xml')"/>
    
     <xsl:template match="/">
       <ItemList>
             <xsl:copy-of select=
             "/*/Item
                 [Product
                 =
                  $vProds/*/ProductCode
               ]
             "/>
        </ItemList>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <ItemList>
        <Item>
            <Product>ABC</Product>
            <Price>10.00</Price>
        </Item>
        <Item>
            <Product>DEF</Product>
            <Price>20.00</Price>
        </Item>
        <Item>
            <Product>GHI</Product>
            <Price>30.00</Price>
        </Item>
        <Item>
            <Product>JKL</Product>
            <Price>40.00</Price>
        </Item>
    </ItemList>
    

    并将提供的 Productlist.xml 存储在 c:\temp\delete:

    <ProductCodeList>
        <ProductCode>ABC</ProductCode>
        <ProductCode>JKL</ProductCode>
    </ProductCodeList>
    

    产生想要的正确结果:

    <ItemList>
       <Item>
          <Product>ABC</Product>
          <Price>10.00</Price>
       </Item>
       <Item>
          <Product>JKL</Product>
          <Price>40.00</Price>
       </Item>
    </ItemList>
    

    【讨论】:

    • 效果很好,但如果保留变量 1 也很好。我有另一个需要变量来进行更复杂的处理,但两者都工作正常。
    • @Hobey:根据您的要求,我更新了我的答案——该解决方案现在使用包含附加文档的文档节点的变量。
    【解决方案2】:

    这样会更好吗?

    <xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />
    <xsl:template match="/">
        <xsl:apply-templates select="/ItemList/Item"/>
    </xsl:template>
    <xsl:template match="/ItemList/Item">
        <xsl:if test="$productCodeList[.=current()/Product]">
            <xsl:copy-of select="."/>
        </xsl:if>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2021-09-16
      • 2017-11-06
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多