2016-07-28 3 views
0

私たちが扱っている製品の1つは、構成情報をXMLとして出力できますが、その出力ファイルでは実際のホスト識別子(名前)各識別子に一種のGUID参照を使用します。XMLで値を検索して別のXMLファイルに置き換えるためのXSLT

私は、ホスト識別子GUIDと実際のホスト識別子(ルックアップ)との間の「マッピング」を含むXMLファイルを作成しました。構成ファイルを通過してすべてのホストを置き換えるXSLTを実装したいホスト識別子名を持つ識別子GUIDを作成しました。これは、作成した他のXMLファイル(lookup.xml)から検索します。

<?xml version="1.0"?> 
<hostids> 

    <hostid name="e687903c-d185-4560-9117-c60f386b76c1">Agent</hostid> 
    <hostid name="b9230962-13ca-4d23-abf8-d3cd1ca4dffc">test2</hostid> 

</hostids> 

、ここで(私はこれを取得するために、いくつかの処理を経て、元のファイルを実行した)設定ファイルは次のようになります。:

<?xml version="1.0"?> 
<resources> 

    <resource><host>e687903c-d185-4560-9117-c60f386b76c1</host><url>/console/**</url></resource> 
    <resource><host>b9230962-13ca-4d23-abf8-d3cd1ca4dffc</host><url>/ServiceByName</url></resource> 

</resources> 
ここ

はlookup.xmlファイルは次のようになります。私はxsltproで働いています

<?xml version="1.0"?> 
<resources> 

    <resource><host>Agent</host><url>/console/**</url></resource> 
    <resource><host>test2</host><url>/ServiceByName</url></resource> 

</resources> 

、ここでは、出力がどのように見えるかですXSLT 1.0であるRedHatマシン上で動作します。

私は、私はここで見つけるいくつかの異なる例のXSLT、例えば:

XSLT "replace" values with another file matching by attribute

が、それらのいずれかが作業を取得することができていないと、この作業を取得しようとしています。

これを達成できる可能性のあるXSLT 1.0の例は誰でも提供できますか?

P.S.これは例を持っていた別のスレッドで、XSLT 1.0の例は私にとってはうまくいきません。私が(要素名にマッチするように変更した後で)それを実行したとき、元のXML全体をラップしたように見えます。

How to use attribute values from another XML file as an element value selection in the current XML

答えて

1

この方法で試してみてください。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="path-to-lookup" select="'lookup.xml'" /> 

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

<xsl:template match="host"> 
    <xsl:copy> 
     <xsl:value-of select="document($path-to-lookup)/hostids/hostid[@name = current()]" /> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

それとも、あなたが好む場合:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="path-to-lookup" select="'lookup.xml'" /> 

<xsl:key name="host" match="hostid" use="@name" /> 

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

<xsl:template match="host"> 
    <xsl:copy> 
     <xsl:variable name="host-id" select="." /> 
     <!-- switch context to lookup document in order to use key --> 
     <xsl:for-each select="document($path-to-lookup)"> 
      <xsl:value-of select="key('host', $host-id)" /> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

michael.hor257kを - 私はあなたの最初のXSLTを試み、それが働きました完璧に!! – user555303

関連する問題