简体   繁体   English

我该如何使用<xsl:param> &amp;<xsl:with-param> 使用 XSLT 1.0 将节点复制到新的 XML</xsl:with-param></xsl:param>

[英]how can I use <xsl:param> & <xsl:with-param> to copy a node into the new XML with XSLT 1.0

I don't even know if this is possible, and I also know that there is a much easier way to do it, but I need to copy a xml node using xsl:param & xsl:with-param.我什至不知道这是否可能,而且我也知道有一种更简单的方法可以做到这一点,但我需要使用 xsl:param 和 xsl:with-param 复制一个 xml 节点。

I have this XML as input:我有这个 XML 作为输入:

<TR>
    <COMPANY name="FIRST">
        <CODE>k</CODE>
    </COMPANY>
    <COMPANY name="SECOND">
        <CODE>h</CODE>
    </COMPANY>
    <COMPANY name="third">
        <CODE>d</CODE>
    </COMPANY>
</TR>

And I have to copy just the element with the attribute name SECOND, and have an output like:而且我必须只复制属性名称为 SECOND 的元素,并且有一个 output ,例如:

<TR>
    <COMPANY name="SECOND">
        <CODE>h</CODE>
    </COMPANY>
</TR>

I have this really simple XSLT that does that:我有一个非常简单的 XSLT 可以做到这一点:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" 
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:apply-templates select="//COMPANY[@name='SECOND']"/>
    </xsl:template>
</xsl:stylesheet>

But as I said I HAVE to use xsl:param & xsl:with-param to copy the node, Im trying this XSLT, and dont know at all if this is even correct or not:但正如我所说,我必须使用 xsl:param 和 xsl:with-param 来复制节点,我正在尝试这个 XSLT,并且根本不知道这是否正确:

<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:param name="key" select="//COMPANY[@name='SECOND']"/>

 <xsl:template match="/">
  <xsl:apply-template>
   <xsl:with-param name="$key"/>
  </xsl:call-template>
</xsl:stylesheet>

Sorry if this is a dumb question, but any help to do what I explained is appreciated.抱歉,如果这是一个愚蠢的问题,但对我解释的任何帮助表示赞赏。

I don't know if that's what your question is about, but if you don't want to hard-code the value "SECOND" into your stylesheet and pass it as a parameter at runtime instead, you could do simply:我不知道这是否是您的问题,但是如果您不想将值"SECOND"硬编码到样式表中并在运行时将其作为参数传递,您可以简单地做:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="theName"/>

<xsl:template match="/TR">
    <xsl:copy>
        <xsl:copy-of select="COMPANY[@name=$theName]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM