我正在尝试用 properties.xml 中的值替换源文件中的变量,该值将源文件中的元素与properties.xml@mybook.01@
中的元素匹配book-name
group id
这是我的源文件:
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2">CLIENT</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
</us-country-factory>
</books>
这是我的 properties.xml 文件:
<variables>
<group id="books/props/Classic">
<variable id="book1">
<mybook.01>123</mybook.01>
</variable>
<variable id="book2">
<mybook.01>789</mybook.01>
</variable>
</group>
<group id="books/props/Classic1">
<variable id="book1">
<mybook.01>ab2</mybook.01>
</variable>
<variable id="book2">
<mybook.01>rt67</mybook.01>
</variable>
</group>
</variables>
所以预期的输出看起来像这样:
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">123</store-property>
<store-property name="book2" type="java.lang.String">789</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">ab2</store-property>
<store-property name="book2" type="java.lang.String">rt67</store-property>
</us-country-factory>
</books>
book-nam
source.xml 中的 e 与group id
from properties.xml匹配store-property
source.xml 中的名称与variable id
from properties.xml匹配@mybook.01@
来自 source.xml 的值被替换为<mybook.01>
来自 properties.xml的值
当 properties.xml 中只有一个组和一个智利节点时,我能够得到这个,但不确定如何使用匹配的模板循环组。
这是我的 xsl 文件:
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:key name="props" match="variable/*"
use="concat(../@id,'
',name(.))"/>
<xsl:template match="book-name">
<xsl:apply-templates select="store-property"/>
</xsl:template>
<xsl:template match="store-property">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="id" select="@name"/>
<xsl:analyze-string select="." regex="@(.*?)@">
<xsl:matching-substring>
<xsl:value-of select="key('props',concat($id,'
',regex-group(1)),
doc('properties.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>