0

ソース ファイル内の変数を、ソース ファイル内の要素と properties.xml 内の要素に@mybook.01@一致する properties.xml の値に置き換えようとしています。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-namgroup idsource.xml の e は from properties.xmlと一致します
  2. store-propertyvariable idsource.xml の名前は from properties.xmlと一致します
  3. @mybook.01@from source.xml は<mybook.01>from properties.xmlの値に置き換えられます

properties.xml と 1 つのチリ ノードにグループしかない場合にこれを取得できましたが、一致したテンプレートでグループをループする方法がわかりません。

これが私の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 に答える