0

ここXSL: how to copy a tree, but remove some nodes? 、しかし、より複雑な XML ファイルがあり、うまく機能しませんでした。

この XML と XSLT 全体は私にとって初めてのことであり、上司から XML (VMWare の OVF ファイル) を別の XML に変換し、いくつかのノードを削除し、他のノードを追加し、情報を更新するタスクが割り当てられました。私は両方の XML ファイルを持っており、私の仕事はそれらを変換する XSLT を設計することです。

元の XML は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Built using IBM Image Construction and Composition Tool, version: 1.2.0.1-20121129-1310-255 on: Oct 18, 2013 12:14:22 -->
<Envelope
    xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
    xmlns:cloudburst="http://www.ibm.com/websphere/rainmaker/2009/3" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
    xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" cloudburst:name="POSTGRES-9.2.4-RHEL-64.X64.xxx.xxx"
    cloudburst:version="1.0.0" cloudburst:build="sample" cloudburst:serviceLevel="0"
    cloudburst:description="BASEIMAGE FOR POSTGRESQL 9.2.4" cloudburst:symbolicName="POSTGRES-9.2.4-RHEL-64.X64.xxx.xxx">
  <References>
    <File ovf:href="en-US-bundle.msg" ovf:id="en-US-bundle.msg" ovf:size="18526"/>
    <File ovf:href="de-DE-bundle.msg" ovf:id="de-DE-bundle.msg" ovf:size="20687"/>
    <File ovf:href="es-ES-bundle.msg" ovf:id="es-ES-bundle.msg" ovf:size="20364"/>
    <File ovf:href="fr-FR-bundle.msg" ovf:id="fr-FR-bundle.msg" ovf:size="20534"/>
    <File ovf:href="it-IT-bundle.msg" ovf:id="it-IT-bundle.msg" ovf:size="20138"/>
    <File ovf:href="ja-JP-bundle.msg" ovf:id="ja-JP-bundle.msg" ovf:size="23116"/>
    <File ovf:href="ko-KR-bundle.msg" ovf:id="ko-KR-bundle.msg" ovf:size="19114"/>
    <File ovf:href="pt-BR-bundle.msg" ovf:id="pt-BR-bundle.msg" ovf:size="20204"/>
    <File ovf:href="zh-CN-bundle.msg" ovf:id="zh-CN-bundle.msg" ovf:size="16875"/>
    <File ovf:href="zh-TW-bundle.msg" ovf:id="zh-TW-bundle.msg" ovf:size="18395"/>
    <File ovf:href="Automation.topology" ovf:id="Automation.topology" ovf:size="196121"/>
    <File ovf:href="Semantic.topology" ovf:id="Semantic.topology" ovf:size="34496"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis.vmdk"
        ovf:size="3129636864"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_1.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_1.vmdk"
        ovf:size="470930944"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_2.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_2.vmdk"
        ovf:size="597504"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_3.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_3.vmdk"
        ovf:size="8147968"/>
    <File ovf:href="default1382090373335.xml" ovf:id="default1382090373335.xml"
        ovf:size="17914" cloudburst:part2Definition="true"/>
    <File ovf:href="default1382090373335C.xml" ovf:id="default1382090373335C.xml"
        ovf:size="15854" cloudburst:part2Definition="true"/>
  </References>
</Envelope>

(これは最初の親ノードだけです。下にもっとありますが、最初の部分を行う方法を知っていると、残りは簡単になると思います)

次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Built using IBM Image Construction and Composition Tool, version: 1.2.0.1-20121129-1310-255 on: Oct 18, 2013 12:14:22 -->
<Envelope
    xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
    xmlns:cloudburst="http://www.ibm.com/websphere/rainmaker/2009/3" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
    xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" cloudburst:name="POSTGRES-9.2.4-RHEL-64.X64.xxx.xxx"
    cloudburst:version="1.0.0" cloudburst:build="sample" cloudburst:serviceLevel="0"
    cloudburst:description="BASEIMAGE FOR POSTGRESQL 9.2.4" cloudburst:symbolicName="POSTGRES-9.2.4-RHEL-64.X64.xxx.xxx">
  <References>
    <File ovf:href="en-US-bundle.msg" ovf:id="en-US-bundle.msg" ovf:size="18526"/>
    <File ovf:href="Automation.topology" ovf:id="Automation.topology" ovf:size="196121"/>
    <File ovf:href="Semantic.topology" ovf:id="Semantic.topology" ovf:size="34496"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis.vmdk"
        ovf:size="3129636864"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_1.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_1.vmdk"
        ovf:size="470930944"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_2.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_2.vmdk"
        ovf:size="597504"/>
    <File ovf:href="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_3.vmdk" ovf:id="RedHat6-4-64-Base-PRB-HARDENEDv1-1-bis_3.vmdk"
        ovf:size="8147968"/>
    <File ovf:href="default1382090373335.xml" ovf:id="default1382090373335.xml"
        ovf:size="17914" cloudburst:part2Definition="true"/>
    <File ovf:href="default1382090373335C.xml" ovf:id="default1382090373335C.xml"
        ovf:size="15854" cloudburst:part2Definition="true"/>
  </References>
</Envelope>

ご覧のとおりFile、最初のノード ( を含む) を除いて、「バンドル」を含むすべてのノードを選択し、それらを削除する必要がありますen-US。それらを選択する私が書いたxPathは

/Envelope/References/File[contains(@ovf:href, 'bundle')][position()>1]

(これには問題がありました。なぜなら、すべての名前空間が原因だと思いますが、Altova XMLspy で試してみたところ、問題なく動作しました)

私は XSL でプログラミングしたことがないので、私が知っているすべてのもの (主に C、Java、PHP、VB.net...) とは少し異なりますが、HTML を知っているので、基本的な構造はわかっています。

Fileそれで、私の質問は、XML 全体をコピーし、そのノードのサブセットを無視する場合、XSL はどのように見えるでしょうか?

これは機能しませんでした。以前にリンクしたSOの回答からコピーしました

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/Envelope/References/File[contains(@href, 'bundle')][position()>1]"/> <!-- this empty template will remove them -->
</xsl:stylesheet>

XSL v1とv2のどちらを使用しても問題ないと思いますが、実際にはそれらの違いはわかりません:D

ありがとう

4

1 に答える 1

1

これは、名前空間が原因です。入力 XML で、デフォルトの名前空間とxmlns="http://schemas.dmtf.org/ovf/envelope/1ovf名前空間を定義しましたxmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1File要素はデフォルトのネームスペースに@href属し、属性はovfネームスペースに属します。これらの名前空間はたまたま同じです。

XSLT で同じ名前空間を定義し、その名前空間を使用して要素と属性を一致させる必要があります。(名前空間の値が入力の適切なものと一致する限り、好きなように名前空間を呼び出すことができることに注意してください。以下ではnsと呼びます。)

File次のスタイルシートは、 「bundle」を含む最初のノードを除くすべてを削除します。

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

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

  <!-- this empty template will remove them -->
  <xsl:template match="ns:Envelope/ns:References/ns:File[contains(@ns:href, 'bundle')][position()>1]"/>
</xsl:stylesheet>
于 2013-10-22T12:05:51.920 に答える