XSLT Document函数在Maven POM上返回空结果

Jan*_*Jan 6 xslt pom.xml maven

问候!

我想通过文档函数从XSLT中的不同Maven POM中提取一些属性.脚本本身工作正常,但只要我在项目标记中有xmlns ="http://maven.apache.org/POM/4.0.0",文档函数就会为POM返回一个空结果.如果我删除它,一切正常.

任何想法如何使这个工作,同时将xmlns属性保留在它所属的位置或为什么这不适用于该属性?

这是我的XSLT的相关部分:

<xsl:template match="abcs">
 <xsl:variable name="artifactCoordinate" select="abc"/>
   <xsl:choose>
        <xsl:when test="document(concat($artifactCoordinate,'-pom.xml'))">
         <abc>
          <ID><xsl:value-of select="$artifactCoordinate"/></ID>
    <xsl:copy-of select="document(concat($artifactCoordinate,'-pom.xml'))/project/properties"/>
   </abc>
         </xsl:when>
            <xsl:otherwise>
       <xsl:message terminate="yes">
           Transformation failed: POM "<xsl:value-of select="concat($artifactCoordinate,'-pom.xml')"/>" doesn't exist. 
       </xsl:message>
      </xsl:otherwise> 

</xsl:choose> 
Run Code Online (Sandbox Code Playgroud)

并且,为了完整性,使用"坏"属性的POM提取:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- ... -->
<properties>
    <proalpha.version>[5.2a]</proalpha.version>
    <proalpha.openedge.version>[10.1B]</proalpha.openedge.version>
    <proalpha.optimierer.version>[1.1]</proalpha.optimierer.version>
    <proalpha.sonic.version>[7.6.1]</proalpha.sonic.version>
</properties>
 </project>
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 11

您的问题是POM提取使用默认命名空间.这意味着这些元素虽然没有前缀,但却在" http://maven.apache.org/POM/4.0.0 "中 - 而不是在"无名称空间"中.

但是,在此XPath表达式中,在XSLT代码中:

document(concat($artifactCoordinate,'-pom.xml'))/project/properties
Run Code Online (Sandbox Code Playgroud)

名字projectproperties没有前缀.XPath始终将未加前缀的名称视为属于"无名称空间".因此,没有找到这样的元素并且没有选择节点.

解决方案:向您添加命名空间定义<xsl:stylesheet>,让我们说:

  xmlns:p="http://maven.apache.org/POM/4.0.0"
Run Code Online (Sandbox Code Playgroud)

然后重写从引用POM节点中的任意表达式元素名称someElementp:someElement.例如:

document(concat($artifactCoordinate,'-pom.xml'))/p:project/p:properties
Run Code Online (Sandbox Code Playgroud)