Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 使用带有正则表达式的xsl分析字符串用属性文件中的值替换变量_Xml_Regex_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

Xml 使用带有正则表达式的xsl分析字符串用属性文件中的值替换变量

Xml 使用带有正则表达式的xsl分析字符串用属性文件中的值替换变量,xml,regex,xslt,xslt-1.0,xslt-2.0,Xml,Regex,Xslt,Xslt 1.0,Xslt 2.0,目前,我正在处理一个项目,该项目需要使用属性文件中的值替换变量,对于这种用法,我认为xsl analyze string将是使用正则表达式替换变量的一个好选项 这是我的source.xml文件: <?xml version="1.0" encoding="UTF-8"?> <projects> <mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:t

目前,我正在处理一个项目,该项目需要使用属性文件中的值替换变量,对于这种用法,我认为xsl analyze string将是使用正则表达式替换变量的一个好选项

这是我的source.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=@mybook.01@
        <!-- properties for def.com -->
        book1.int=@mybook.01@
        <!-- properties for ghi.com -->
        book1.qa=@mybook.01@
        <!-- properties for jkl.com -->
        book1.prod=@mybook.01@
    </attribute>
</mbean>
<projects>
<?xml version="1.0" encoding="UTF-8"?>
<variables>
    <variable id="book1.dev">
        <mybook.01>123</mybook.01>
        <mybook.02>456</mybook.02>
    </variable>
    <variable id="book1.int">
        <mybook.01>789</mybook.01>
        <mybook.02>346</mybook.02>
    </variable>
    <variable id="book1.qa">
        <mybook.01>ab2</mybook.01>
        <mybook.02>45ff</mybook.02>
    </variable>
    <variable id="book1.prod">
        <mybook.01>rt67</mybook.01>
        <mybook.02>hgj8</mybook.02>
    </variable> 
</variables>
根据@Martin的输入,我正在添加其他信息:

源xml有一个名为:
“@mybook.01@”

我正在尝试获取properties.xml文件到output.xml中所有变量的

预期的output.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=123
        <!-- properties for def.com -->
        book1.int=789
        <!-- properties for ghi.com -->
        book1.qa=ab2
        <!-- properties for jkl.com -->
        book1.prod=rt67
    </attribute>
</mbean>
<projects>

book1.dev=123
book1.int=789
book1.qa=ab2
book1.prod=rt67
样式表

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

<xsl:key name="props" match="variable/*"
         use="concat(../@id,'&#xd;',name(.))"/>
<xsl:template match="attribute">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:variable name="id" select="../@name"/>
    <xsl:analyze-string select="." regex="([\w.]+)=@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat(regex-group(1), '=', key('props',concat(regex-group(1),'&#xd;',regex-group(2)),
                      doc('test2013081402.xml')))"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:copy>
</xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
如果要保留注释,则需要更改模板以处理
属性
元素节点的
文本
子节点:

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:key name="props" match="variable/*"
         use="concat(../@id,'&#xd;',name(.))"/>

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

<xsl:template match="attribute/text()">
    <xsl:analyze-string select="." regex="([\w.]+)=@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat(regex-group(1), '=', key('props',concat(regex-group(1),'&#xd;',regex-group(2)),
                      doc('test2013081402.xml')))"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>


</xsl:stylesheet>

上面最新的样式表输出

<?xml version="1.0" encoding="UTF-8"?><projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=123
        <!-- properties for def.com -->
        book1.int=789
        <!-- properties for ghi.com -->
        book1.qa=ab2
        <!-- properties for jkl.com -->
        book1.prod=rt67
    </attribute>
</mbean>
</projects>

book1.dev=123
book1.int=789
book1.qa=ab2
book1.prod=rt67

name属性的值是
Properties
,因此当您执行
concat($id,
;',regex group(1))
时,
$id
属性
,但在引用的XML文档中键入
@id
值,如
book1.dev
。这样,
调用就找不到任何匹配的值。我不熟悉文件格式,从你的帖子中我也不确定你想要哪个键定义,因此你需要更详细地解释你想要引用哪些值。更新了问题。谢谢你的代码,有没有办法让我们在输出文件中保留注释,即使处理完文件。@phani,我编辑了我的答案,以建议对样式表进行更改。我得到以下错误:XPTY0004:不允许将多个项的序列作为concat()的第三个参数。我使用使用使用Saxon 9.5 Java发布的示例测试了发布的代码。我不知道为什么会出现错误,除非您更改了XSLT和/或输入。编辑您的问题,提供您使用的示例,并告诉我们您使用的XSLT2.0处理器。我使用的是XSLT2.0,SAXON-HE 9.5.1-1版本,xsl文件中没有任何更改。当我删除
”和#xd;'从联系人字符串中,我看不到任何错误,但在输出文件中获取空值。
<projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
 name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=@mybook.01@
        <!-- properties for def.com -->
        book1.int=@mybook.01@
        <!-- properties for ghi.com -->
        book1.qa=@mybook.01@
        <!-- properties for jkl.com -->
        book1.prod=@mybook.01@
    </attribute>
</mbean>
</projects>
<?xml version="1.0" encoding="UTF-8"?><projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">

        book1.dev=123

        book1.int=789

        book1.qa=ab2

        book1.prod=rt67
    </attribute>
</mbean>
</projects>
<variables>
    <variable id="book1.dev">
        <mybook.01>123</mybook.01>
        <mybook.02>456</mybook.02>
    </variable>
    <variable id="book1.int">
        <mybook.01>789</mybook.01>
        <mybook.02>346</mybook.02>
    </variable>
    <variable id="book1.qa">
        <mybook.01>ab2</mybook.01>
        <mybook.02>45ff</mybook.02>
    </variable>
    <variable id="book1.prod">
        <mybook.01>rt67</mybook.01>
        <mybook.02>hgj8</mybook.02>
    </variable> 
</variables>
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:key name="props" match="variable/*"
         use="concat(../@id,'&#xd;',name(.))"/>

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

<xsl:template match="attribute/text()">
    <xsl:analyze-string select="." regex="([\w.]+)=@(.*?)@">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat(regex-group(1), '=', key('props',concat(regex-group(1),'&#xd;',regex-group(2)),
                      doc('test2013081402.xml')))"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>


</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?><projects>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="myprops.values:type=Service,name=MyProp">
    <attribute name="Properties">
        <!-- properties for abc.com -->
        book1.dev=123
        <!-- properties for def.com -->
        book1.int=789
        <!-- properties for ghi.com -->
        book1.qa=ab2
        <!-- properties for jkl.com -->
        book1.prod=rt67
    </attribute>
</mbean>
</projects>