0

我正在尝试用 properties.xml 中的值替换源文件中的变量,该值将源文件中的元素与properties.xml@mybook.01@中的元素匹配book-namegroup 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>
  1. book-namsource.xml 中的 e 与group idfrom properties.xml匹配
  2. store-propertysource.xml 中的名称与variable idfrom properties.xml匹配
  3. @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,'&#xd;',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,'&#xd;',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>
4

1 回答 1

1

你可以做这样的事情而不是......

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="props" select="document('properties.xml')"/>

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

    <xsl:template match="store-property[matches(.,'^@')]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="$props/*/group[@id=current()/../book-name]/variable[@id=current()/@name]/*[local-name()=tokenize(current(),'@')[2]]"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XML 输出(使用提供的 XML 示例)

<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>
      <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">ab2</store-property>
      <store-property name="book2" type="java.lang.String">rt67</store-property>
   </us-country-factory>
</books>
于 2013-09-26T19:26:39.723 回答