2016-07-21 8 views
0

I以下の入力XMLを持っている:XSLTの問題:要素属性に変換

<?xml version="1.0" encoding="UTF-8"?> 
<class xmlns:xfa="soommmeee-tteexxxxtttttt"> 
    <students> 
    ------ 
    ------ 
    ------ 
    ------ 
    </students> 
    <students> 
     <id>B</id> 
     <name>jzvxcbhjnba</name> 
     <description>mjZSVxcj</description> 
     <student> 
      <id>3</id> 
      <refObjectId>m8</refObjectId> 
      <subject> 
       <id>91</id> 
       <name>humanities</name> 
       <friend>hercules</friend> 
       <firstname> 
        <textContent> 
         <Id>m954</Id> 
         <title xfa:contentType="text/html"> 
          <body>Title</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Current</body> 
         </text> 
         <Key>Title</Key> 
        </textContent> 
        <textContent> 
         <Id>m9542</Id> 
         <title xfa:contentType="text/html"> 
          <body>SubTitle</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Sr</body> 
         </text> 
         <Key>SubTitle</Key> 
        </textContent> 
        <isDisplay>false</isDisplay> 
       </firstname> 
       <lastname> 
        <Detail> 
         <lastNameDetail> 
          <fragId>aksbgdk</fragId> 
          <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
          <zmdcb>slzdhfksjfml</zmdcb> 
         </lastNameDetail> 
        </Detail> 
       </lastname> 
      </subject> 
     </student> 
     <student> 
      <id>3</id> 
      <refObjectId>m8</refObjectId> 
      <subject> 
       <id>91</id> 
       <name>humanities</name> 
       <friend>aladin</friend> 
       <firstname> 
        <textContent> 
         <Id>m954</Id> 
         <title xfa:contentType="text/html"> 
          <body>Title</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Current</body> 
         </text> 
         <Key>Title</Key> 
        </textContent> 
        <textContent> 
         <Id>m9542</Id> 
         <title xfa:contentType="text/html"> 
          <body>SubTitle</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Sr</body> 
         </text> 
         <Key>SubTitle</Key> 
        </textContent> 
        <isDisplay>false</isDisplay> 
       </firstname> 
       <lastname> 
        <Detail> 
         <lastNameDetail> 
          <fragId>aksbgdk</fragId> 
          <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
          <zmdcb>slzdhfksjfml</zmdcb> 
         </lastNameDetail> 
        </Detail> 
       </lastname> 
      </subject> 
     </student> 
     <student> 
      <id>3</id> 
      <refObjectId>m8</refObjectId> 
      <subject> 
       <id>91</id> 
       <name>humanities</name> 
       <friend>aladin</friend> 
       <firstname> 
        <textContent> 
         <Id>m954</Id> 
         <title xfa:contentType="text/html"> 
          <body>Title</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Current</body> 
         </text> 
         <Key>Title</Key> 
        </textContent> 
        <textContent> 
         <Id>m9542</Id> 
         <title xfa:contentType="text/html"> 
          <body>SubTitle</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>Sr</body> 
         </text> 
         <Key>SubTitle</Key> 
        </textContent> 
        <isDisplay>false</isDisplay> 
       </firstname> 
       <lastname> 
        <Detail> 
         <lastNameDetail> 
          <fragId>aksbgdk</fragId> 
          <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
          <zmdcb>slzdhfksjfml</zmdcb> 
         </lastNameDetail> 
        </Detail> 
       </lastname> 
      </subject> 
     </student> 
     <student> 
      <id>4</id> 
      <refObjectId>m3</refObjectId> 
      <subject> 
       <id>75</id> 
       <name>PCM</name> 
       <friend>hercules</friend> 
       <firstname> 
        <textContent> 
         <Id>j4423</Id> 
         <title xfa:contentType="text/html"> 
          <body>Title</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>dfzxsdbjchA</body> 
         </text> 
         <Key>zdbgcfjkbna</Key> 
        </textContent> 
        <textContent> 
         <Id>m95</Id> 
         <title xfa:contentType="text/html"> 
          <body>zdjbhfjkcda</body> 
         </title> 
         <text xfa:contentType="text/html"> 
          <body>sdjkhzjk</body> 
         </text> 
         <Key>mzsdjkbjk</Key> 
        </textContent> 
        <isDisplay>true</isDisplay> 
       </firstname> 
       <lastname> 
        <Detail> 
         <lastNameDetail> 
          <fragId>sdbnfn</fragId> 
          <znjdvgsc>masdjkljk</znjdvgsc> 
          <zmdcb>msjkldbhfjkn</zmdcb> 
         </lastNameDetail> 
        </Detail> 
       </lastname> 
      </subject> 
     </student> 
    </students> 
    <students> 
    -------- 
    ------- 
    --------- 
    -------- 
    </students> 
    <faculty> 
     <configuration> 
      <Type>Output</Type> 
      <Key>Font</Key> 
      <Value>10pt</Value> 
     </configuration> 
     <configuration> 
      <Type>Out</Type> 
      <Key>Text</Key> 
      <Value>127,127,127</Value> 
     </configuration> 
     <configuration> 
      <Type>put</Type> 
      <Key>Slide</Key> 
      <Value>18pt</Value> 
     </configuration> 
     <configuration> 
      <Type>utp</Type> 
      <Key>Slide</Key> 
      <Value>127</Value> 
     </configuration> 
    </faculty> 
    <Info /> 
    <Dean> 
     <name>zcdfjkaqbkd</name> 
    </Dean> 
</class> 

私は何をしたいであるかに存在してもしなくてもよい、すべての親ノードの子ノード「ID」すべてのツリーは親ノードの属性になり、idノードはxmlから永久に削除されます。私は以下、XSLT 1.0を使用しています

は私のXSLファイルです:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" 
    version="1.0"> 
    <xsl:output indent="yes" /> 
    <xsl:strip-space elements="*"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates 
      select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="id" /> 
<xsl:template match="subject"> 
    <subject id="{id}"> 
     <xsl:copy> 
      <xsl:apply-templates select="child::node()" /> 
     </xsl:copy>   
    </subject> 
</xsl:template> 
<xsl:template match="students"> 
    <students id="{id}"> 
     <xsl:copy> 
      <xsl:apply-templates select="child::node()" /> 
     </xsl:copy> 
    </students> 
</xsl:template> 
<xsl:template match="student"> 
    <student id="{id}"> 
     <xsl:copy> 
      <xsl:apply-templates select="child::node()" /> 
     </xsl:copy> 
    </student> 
</xsl:template> 
</xsl:stylesheet> 

私のXSLTは必要に応じて、それは親ノードに属性を作っていることをやっている、それはあまりにとしてのidノードを削除してしかし、それは親ノードの新しいコピーを作成されたい 私の現在の出力が添付されています

<?xml version="1.0" encoding="UTF-8"?><class xmlns:xfa="soommmeee-tteexxxxtttttt"> 
<students id="A"> 
<students> 
----- 
----- 
------ 
</students> 
</students> 
<students id="B"> 
<students> 
<name>jzvxcbhjnba</name> 
<description>mjZSVxcj</description> 
<student id="3"> 
<student> 
<refObjectId>m8</refObjectId> 
<subject id="91"> 
<subject> 
<name>humanities</name> 
<friend>hercules</friend> 
<firstname> 
<textContent> 
<Id>m954</Id> 
<title xfa:contentType="text/html"> 
<body>Title</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Current</body> 
</text> 
<Key>Title</Key> 
</textContent> 
<textContent> 
<Id>m9542</Id> 
<title xfa:contentType="text/html"> 
<body>SubTitle</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Sr</body> 
</text> 
<Key>SubTitle</Key> 
</textContent> 
<isDisplay>false</isDisplay> 
</firstname> 
<lastname> 
<Detail> 
<lastNameDetail> 
<fragId>aksbgdk</fragId> 
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
<zmdcb>slzdhfksjfml</zmdcb> 
</lastNameDetail> 
</Detail> 
</lastname> 
</subject> 
</subject> 
</student> 
</student> 
<student id="3"> 
<student> 
<refObjectId>m8</refObjectId> 
<subject id="91"> 
<subject> 
<name>humanities</name> 
<friend>aladin</friend> 
<firstname> 
<textContent> 
<Id>m954</Id> 
<title xfa:contentType="text/html"> 
<body>Title</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Current</body> 
</text> 
<Key>Title</Key> 
</textContent> 
<textContent> 
<Id>m9542</Id> 
<title xfa:contentType="text/html"> 
<body>SubTitle</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Sr</body> 
</text> 
<Key>SubTitle</Key> 
</textContent> 
<isDisplay>false</isDisplay> 
</firstname> 
<lastname> 
<Detail> 
<lastNameDetail> 
<fragId>aksbgdk</fragId> 
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
<zmdcb>slzdhfksjfml</zmdcb> 
</lastNameDetail> 
</Detail> 
</lastname> 
</subject> 
</subject> 
</student> 
</student> 
<student id="3"> 
<student> 
<refObjectId>m8</refObjectId> 
<subject id="91"> 
<subject> 
<name>humanities</name> 
<friend>aladin</friend> 
<firstname> 
<textContent> 
<Id>m954</Id> 
<title xfa:contentType="text/html"> 
<body>Title</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Current</body> 
</text> 
<Key>Title</Key> 
</textContent> 
<textContent> 
<Id>m9542</Id> 
<title xfa:contentType="text/html"> 
<body>SubTitle</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>Sr</body> 
</text> 
<Key>SubTitle</Key> 
</textContent> 
<isDisplay>false</isDisplay> 
</firstname> 
<lastname> 
<Detail> 
<lastNameDetail> 
<fragId>aksbgdk</fragId> 
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc> 
<zmdcb>slzdhfksjfml</zmdcb> 
</lastNameDetail> 
</Detail> 
</lastname> 
</subject> 
</subject> 
</student> 
</student> 
<student id="4"> 
<student> 
<refObjectId>m3</refObjectId> 
<subject id="75"> 
<subject> 
<name>PCM</name> 
<friend>hercules</friend> 
<firstname> 
<textContent> 
<Id>j4423</Id> 
<title xfa:contentType="text/html"> 
<body>Title</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>dfzxsdbjchA</body> 
</text> 
<Key>zdbgcfjkbna</Key> 
</textContent> 
<textContent> 
<Id>m95</Id> 
<title xfa:contentType="text/html"> 
<body>zdjbhfjkcda</body> 
</title> 
<text xfa:contentType="text/html"> 
<body>sdjkhzjk</body> 
</text> 
<Key>mzsdjkbjk</Key> 
</textContent> 
<isDisplay>true</isDisplay> 
</firstname> 
<lastname> 
<Detail> 
<lastNameDetail> 
<fragId>sdbnfn</fragId> 
<znjdvgsc>masdjkljk</znjdvgsc> 
<zmdcb>msjkldbhfjkn</zmdcb> 
</lastNameDetail> 
</Detail> 
</lastname> 
</subject> 
</subject> 
</student> 
</student> 
</students> 
</students> 
<students id="B"> 
<students> 
---- 
----- 
----- 
---- 
</students> 
</students> 
<faculty> 
<configuration> 
<Type>Output</Type> 
<Key>Font</Key> 
<Value>10pt</Value> 
</configuration> 
<configuration> 
<Type>Out</Type> 
<Key>Text</Key> 
<Value>127,127,127</Value> 
</configuration> 
<configuration> 
<Type>put</Type> 
<Key>Slide</Key> 
<Value>18pt</Value> 
</configuration> 
<configuration> 
<Type>utp</Type> 
<Key>Slide</Key> 
<Value>127</Value> 
</configuration> 
</faculty> 
<Info/> 
<Dean> 
<name>zcdfjkaqbkd</name> 
</Dean> 
</class> 

助けてください...私はここで立ち往生しています!

答えて

2

xsl:copyを使用しているため、新しいノードを手動で作成するため、「親」ノードのコピーが作成されています。 xsl:copyは現在のノードのコピーを作成するので、2つになります。手動で作成するものは<student id="{id}">、コピーするものはxsl:copyです。

だから、あなたのテンプレートは本当にこのようになります。

<xsl:template match="subject"> 
    <subject id="{id}"> 
     <xsl:apply-templates /> 
    </subject> 
</xsl:template> 

しかし、あなたは子供idノードで任意の要素を一致させるために、あなたのXSLTは、より汎用的にすることができます。

完全に罰金働い1 ....あなたの助けに感謝

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" 
    version="1.0"> 
    <xsl:output indent="yes" /> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="id" /> 

    <xsl:template match="*[id]"> 
     <xsl:copy> 
      <xsl:attribute name="id"> 
       <xsl:value-of select="id" /> 
      </xsl:attribute> 
      <xsl:apply-templates /> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

このXSLTをお試しください – NealGul

関連する問題