Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 用XSLT替换内容(按变量)_Xml_Xslt - Fatal编程技术网

Xml 用XSLT替换内容(按变量)

Xml 用XSLT替换内容(按变量),xml,xslt,Xml,Xslt,我想用预定义的变量替换文件路径 路径变量的源(仅两个示例-在期望的结果中,它将更多): C:\\example\\fileA.xml C:\\example\\fileB.xml 具有路径的标记的源: <topic insetPath="C:\\example\\fileA.xml" flowTag="title"/> <topic insetPath="C:\\example\\fileB.xml" flowTag="text"/>        所需的X

我想用预定义的变量替换文件路径

路径变量的源(仅两个示例-在期望的结果中,它将更多):


C:\\example\\fileA.xml
C:\\example\\fileB.xml
具有路径的标记的源:

<topic insetPath="C:\\example\\fileA.xml" flowTag="title"/>
<topic insetPath="C:\\example\\fileB.xml" flowTag="text"/>    

    
所需的XML输出:

<topic flowTag="title"><xsl:attribute name="insetPath"><xsl:value-of select="$fileA"/></xsl:attribute></topic>
<topic flowTag="text"><xsl:attribute name="insetPath"><xsl:value-of select="$fileB"/></xsl:attribute>

My variables.xsl如下所示:

<xsl:stylesheet>
  <xsl:variable name='fileA'>
    <xsl:text>C:\\example\\fileA.xml</xsl:text> 
  </xsl:variable>

  <xsl:variable name='fileB'>
    <xsl:text>C:\\example\\fileB.xml</xsl:text> 
 </xsl:variable>
</xsl:stylesheet>

C:\\example\\fileA.xml
C:\\example\\fileB.xml
我不确定您是否应该为此使用(纯)XSLT,因为XSLT缺少允许良好运行时复杂性的容器,但代码非常简单:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Load the variables from a separate XML file.
       Note that this "xsl:" refers to the alias in *this* document, regardless of what alias, if any, is used in varaibles.xml -->
  <xsl:variable name="variables" select="document('variables.xml')/*/xsl:variable/xsl:text/text()"/>
  <!-- Simple modification of the identity transform -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <!-- Must emit attributes first, then elements afterwards. Thus, WET. -->
      <xsl:apply-templates select="@*[not(.=$variables)]"/>
      <xsl:for-each select="@*[.=$variables]">
        <xsl:variable name="attr" select="string(.)"/>
        <!-- Outputting XSLT itself is slightly painful -->
        <xsl:element namespace="http://www.w3.org/1999/XSL/Transform" name="attribute">
          <xsl:attribute name="name">
            <xsl:value-of select="name()"/>
          </xsl:attribute>
          <xsl:element namespace="http://www.w3.org/1999/XSL/Transform" name="value-of">
            <xsl:attribute name="select">
              <xsl:text>$</xsl:text>
              <!-- go up from the text() node, to the xsl:text element, to the xsl:variable element -->
              <xsl:value-of select="$variables[.=$attr]/../../@name"/>
            </xsl:attribute>
          </xsl:element>
        </xsl:element>
      </xsl:for-each>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

$
我不确定您是否应该为此使用(纯)XSLT,因为XSLT缺少允许良好运行时复杂性的容器,但代码非常简单:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Load the variables from a separate XML file.
       Note that this "xsl:" refers to the alias in *this* document, regardless of what alias, if any, is used in varaibles.xml -->
  <xsl:variable name="variables" select="document('variables.xml')/*/xsl:variable/xsl:text/text()"/>
  <!-- Simple modification of the identity transform -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <!-- Must emit attributes first, then elements afterwards. Thus, WET. -->
      <xsl:apply-templates select="@*[not(.=$variables)]"/>
      <xsl:for-each select="@*[.=$variables]">
        <xsl:variable name="attr" select="string(.)"/>
        <!-- Outputting XSLT itself is slightly painful -->
        <xsl:element namespace="http://www.w3.org/1999/XSL/Transform" name="attribute">
          <xsl:attribute name="name">
            <xsl:value-of select="name()"/>
          </xsl:attribute>
          <xsl:element namespace="http://www.w3.org/1999/XSL/Transform" name="value-of">
            <xsl:attribute name="select">
              <xsl:text>$</xsl:text>
              <!-- go up from the text() node, to the xsl:text element, to the xsl:variable element -->
              <xsl:value-of select="$variables[.=$attr]/../../@name"/>
            </xsl:attribute>
          </xsl:element>
        </xsl:element>
      </xsl:for-each>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

$

您的“所需XML输出”中有XSL标记。是否正确?@o11c请使用此空间对问题进行评论,而不是记录您的活动。是的,所需的XML输出中有XSL标记。您的“所需XML输出”中有XSL标记。是否正确?@o11c请使用此空间对问题进行评论,而不是记录您的活动。是的,所需的XML输出具有XSL标记。