簡體   English   中英

使用 xslt 更新 pom.xml 元素

[英]updating pom.xml elements using xslt

我正在嘗試使用 xslt 更新 pom.xml 中的 finalName、artifactId 和名稱。 我不明白 xslt 轉換,但我發現了這個: how to modify xml file using 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:template match="//artifactId">
    <artifactId>aaaaaaaaaaaaaaaaaaaaaaaaaaa123</artifactId>
  </xsl:template>
</xsl:stylesheet>

並使用以下方法構建它:

xsltproc template.xsl pom.xml > modified-pom.xml

但是由於某些我不知道的原因,xpath //artifactId似乎沒有被匹配和替換。 我也嘗試過:僅artifactIdproject/artifactId/project/artifactId等,但沒有匹配項。 記住使用需要特定名稱空間的xmlstalet的教訓,這似乎可能與名稱空間相關,但我真的不知道如何修復它,因為 man xsltproc 似乎以某種方式被破壞/不反映命令現實。 有人可以幫助錯誤在哪里嗎?

編輯:根據評論的要求,添加了部分解決的 state:

作為一個最低限度的工作示例輸入文件,我們可以考慮這個 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <artifactId>toUpdate</artifactId>
  <name>toUpdate</name>

  <properties>
    <finalName>toUpdate</finalName>
  </properties>
</project>

我正在使用現在的模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://maven.apache.org/POM/4.0.0">
  <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="/m:project/m:artifactId">
    <artifactId>updated</artifactId>
  </xsl:template>
  <xsl:template match="/m:project/m:name">
    <name>updated</name>
  </xsl:template>
  <xsl:template match="/m:project/m:properties/m:finalName">
    <finalName>updated</finalName>
  </xsl:template>
</xsl:stylesheet>

並使用命令替換它: xsltproc template.xsl min.xml > out.xml

output 是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <artifactId xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</artifactId>
  <name xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</name>
  <properties>
    <finalName xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</finalName>
  </properties>
</project>

我想擺脫部分xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0"

如果我將模板中的替換更新為: <m:finalName>updated</m:finalName>我沒有得到正確的結果,而是:

<m:finalName xmlns:m="http://maven.apache.org/POM/4.0.0">updated</m:finalName>

試圖聲明全局命名空間而不是m也對我沒有幫助,因為它甚至沒有構建。

預期的 output 輸入與toUpdate替換為updated

考慮以下最小化的示例:

XML

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <artifactId>toUpdate</artifactId>
    <name>toUpdate</name>
    <properties>
        <finalName>toUpdate</finalName>
    </properties>
</project>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:m="http://maven.apache.org/POM/4.0.0"
exclude-result-prefixes="m">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="m:artifactId">
    <artifactId>updated</artifactId>
</xsl:template>

</xsl:stylesheet>

結果

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <artifactId>updated</artifactId>
  <name>toUpdate</name>
  <properties>
    <finalName>toUpdate</finalName>
  </properties>
</project>

請注意xsl:styleshhet開始標記中的兩個名稱空間聲明。 一種是用於尋址源 XML 中的元素。 另一個將任何文字結果元素放在目標命名空間的樣式表中。


如果您只想替換元素的文本內容而不重命名它,您可以將其簡化為:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://maven.apache.org/POM/4.0.0"
exclude-result-prefixes="m">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="m:artifactId/text()">updated</xsl:template>

</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM