代码之家  ›  专栏  ›  技术社区  ›  GreyCloud

带修改的XSLT复制

  •  2
  • GreyCloud  · 技术社区  · 15 年前

    我希望能够做到以下几点: 我是xsl:for-each'遍历一组类型的节点

       <node>
         <data> unknown unstructured xml </data>
         <owner></owner>
       </node>
    

    我希望能够输出

       <node>
         <data> unknown unstructured xml </data>
         <!--RESULT of calling an XSL template with certain parameters -->
       </node>
    

    从我的搜索到目前为止,我认为我可以做一些类似的事情 here

        <xsl:copy> 
            <xsl:apply-template name="findownerdetails">
               <xsl:with-param name="data" select="something" />
            </xsl:apply-template> 
        </xsl:copy> 
    

    但这显然是无效的。有什么建议如何让这个工作或取得类似的东西?我恐怕我不能只调用应用模板,因为我想要的模板将取决于一些数据,我正在建立,因为我为每个通过节点元素列表。

    有什么建议吗

    2 回复  |  直到 8 年前
        1
  •  1
  •   Jon Hanna    15 年前
    <xsl:template match="node">
      <node>
        <xsl:copy-of select="data"/>
        <!-- assuming this next bit in your question example
        is something you are happy with -->
        <xsl:call-template name="findownerdetails">
          <xsl:with-param name="data" select="something" />
        </xsl:call-template> 
      </node>
    </xsl:template>
    
        2
  •  5
  •   Dimitre Novatchev    15 年前

    这是使用和重写 identity rule :

    <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:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="owner">
        <owner-details>
          <xsl:value-of select="."/>
        </owner-details>
     </xsl:template>
    </xsl:stylesheet>
    

    (基于提供的带有附加属性和所有者详细信息的XML文档):

    <node attr1="x" attr2="y">
        <data> unknown unstructured xml </data>
        <owner>
            <details>
                <name>John Smith </name>
                <profession>XSLT programmer</profession>
            </details>
        </owner>
    </node>
    

    :

    <node attr1="x" attr2="y">
       <data> unknown unstructured xml </data>
       <owner-details>John Smith XSLT programmer</owner-details>
    </node>
    

    注意事项 :

    1. 标识模板复制每个节点 以递归方式“按原样”保存在文档中。

    2. . 任何模板,其匹配模式比标识模板的匹配模式更具体,都会覆盖它——XSLT处理器总是为节点选择最具体的匹配模板。

    3. 使用和重写标识规则是最基本的 ,最强大,最一般

    4. OP在一篇评论中建议,这个解决方案不允许传递参数。这不是真的 . 任何模板(包括标识规则)都可以编写为具有参数——当需要时。在这种特殊情况下 必须通过模板传递参数。

    5. 模板匹配 owner 调用另一个模板 --所有特定于所有者的处理都可以在这里完成。

    推荐文章