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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
Xslt 使用XSL修改标记内容_Xslt - Fatal编程技术网

Xslt 使用XSL修改标记内容

Xslt 使用XSL修改标记内容,xslt,Xslt,我有以下XML: <dependency> <groupId>blabla1</groupId> <artifactId>test1</artifactId> <version>if strArtifactId equals test1, put iVersion value here</version> </dependency>

我有以下XML:

    <dependency>
        <groupId>blabla1</groupId>
        <artifactId>test1</artifactId>
        <version>if strArtifactId equals test1, put iVersion value here</version>
    </dependency>

    <dependency>
        <groupId>blabla2</groupId>
        <artifactId>test2</artifactId>
        <version>if strArtifactId equals test2, put iVersion value here</version>
    </dependency>

    <dependency>
        <groupId>blabla3</groupId>
        <artifactId>test3</artifactId>
        <version>if strArtifactId equals test3, put iVersion value here</version>
    </dependency>
以及以下XSL:

   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="strArtifactID"/>
  <xsl:param name="iVersion"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="artifactId">

    <xsl:copy>
      <xsl:value-of select="$strArtifactID"/>
    </xsl:copy>

  </xsl:template>

  <xsl:template match="version">
    <xsl:copy>
      <xsl:value-of select="$iVersion"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
我需要做的是用我的参数替换依赖项的版本标记

strArtifactId:选择要修改的依赖项版本

版本:我想要的版本

这两个参数都由GUI提供

问题是:当我启动代码时,它只复制原始xml,但没有任何更改


有什么帮助吗?

您提供的XML不完整,因此我在代码示例中的节点周围添加了一个节点,使其成为有效的XML

因此,您的XML如下所示:

<root>
  <dependency>
    <groupId>blabla1</groupId>
    <artifactId>test1</artifactId>
    <version>if strArtifactId equals test1, put iVersion value here</version>
  </dependency>
  <dependency>
    <groupId>blabla2</groupId>
    <artifactId>test2</artifactId>
    <version>if strArtifactId equals test2, put iVersion value here</version>
  </dependency>
  <dependency>
    <groupId>blabla3</groupId>
    <artifactId>test3</artifactId>
    <version>if strArtifactId equals test3, put iVersion value here</version>
  </dependency>
</root>
以下是XSL样式表,它将根据符合上述示例的XML为您提供所需的结果:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:param name="strArtifactID" />
  <xsl:param name="iVersion" />

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="dependency">
    <dependency>
      <xsl:copy-of select="groupId" />
      <xsl:copy-of select="artifactId" />
      <xsl:choose>
        <xsl:when test="artifactId = $strArtifactID">
          <version><xsl:value-of select="format-number($iVersion, '#.0')" /></version>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="version" />
        </xsl:otherwise>
      </xsl:choose>
    </dependency>
  </xsl:template>

</xsl:stylesheet>
因此,样式表中发生的是:

您的'strArtifactID'和'ivervision'参数值由您提到的GUI设置,我在名称值后使用select='test1'和select=1.0为它们赋值,仅用于测试目的。 标识模板生成XML的精确副本。 节点将按照步骤4、5、6所需的格式重建。 只复制和节点,因为它们的内容不会更改。 当a中的节点值与“strArtifactID”参数值的值匹配时,将创建一个包含“iVersion”参数值的节点,我假设该参数值为数字,并已格式化为保留小数点后一位。 当中的节点值与“strArtifactID”参数值不匹配时,将复制现有节点。
需要输出完整的输入XML。上面的xsl是正确的。我不能评论您的Java代码,但这可能会对您有所帮助:谢谢,但是当我调试xsl代码时,它永远不会达到它停留在xml中的程度。您能给我看一下您的完整xml吗?让xsl为您工作应该像更改“匹配”值一样简单,但我需要查看您的完整xml来帮助您完成这项工作。我必须修改我的xml,以便在完成时可以。如果有其他问题,我会张贴。多谢各位