2

XSLT 変換に取り組んでいます。タグと呼ばれるタグがあるソースファイルがあります。

ソース xml がこのようなものであると考えてください。

<ABC>
    <Name>Some Name </Name>
    <ID>Some ID</ID>
    <Address>Some Address</Address>
    <Place>Some Place</Place>
    <ID>Some ID</ID>
    <Name>Some Name </Name>
    <Name>Some Name </Name>     

</ABC>

ルール:

  ABC is parent Tag which has 4 child tags. Name, ID, Address, Place. 
  These child tags can occur many times and in any ordrer.
  Upon reading the tag , I want to change the name of the tag, and do some processing on the value present in the tag.
  The input XML may have child tags in any order, and many times.
  I want to write a common XSLT which will read the child tags in the order in which they occur, and display them as given under.

このような出力を表示したい。

        <Frame:Name>
                   <text>Some Name</text>
        </Frame:Name>
   <Frame:ID>
                   <text>Some ID</text>
        </Frame:ID>
        <Frame:ADdrress>
                   <text>Some Address</text>
        </Frame:Address>
        <Frame:Place>
                   <text>Some Place</text>
        </Frame:Place>
   <Frame:ID>
                   <text>Some ID</text>
        </Frame:ID>
       <Frame:Name>
                   <text>Some Name</text>
        </Frame:Name>
       <Frame:Name>
                   <text>Some Name</text>
        </Frame:Name>

どうやってそれができるのか、私は完全に感銘を受けました。

ソース XML で子要素の出現順序が変更された場合、出力 XML にも反映される必要があります。

誰かがレビューを共有できますか。

ありがとうございました。

4

2 に答える 2

1

このような問題を解決する XSLT の方法は、テンプレートを使用することです。xsl:chooseその結果、通常は、xsl:whenxsl:otherwisexsl:ifまたはがなく、はるかにシンプルで短く、理解しやすいコードになりますxsl:for-each

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:Frame="some:undefined namespace">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
   <t><xsl:apply-templates/></t>
 </xsl:template>

 <xsl:template match="*/*">
     <xsl:element name="Frame:{name()}" namespace="some:undefined namespace">
       <text><xsl:apply-templates /></text>
     </xsl:element>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<ABC>
    <Name>Some Name </Name>
    <ID>Some ID</ID>
    <Address>Some Address</Address>
    <Place>Some Place</Place>
    <ID>Some ID</ID>
    <Name>Some Name </Name>
    <Name>Some Name </Name>
</ABC>

必要な正しい結果が生成されます。

<t xmlns:Frame="some:undefined namespace">
   <Frame:Name>
      <text>Some Name </text>
   </Frame:Name>
   <Frame:ID>
      <text>Some ID</text>
   </Frame:ID>
   <Frame:Address>
      <text>Some Address</text>
   </Frame:Address>
   <Frame:Place>
      <text>Some Place</text>
   </Frame:Place>
   <Frame:ID>
      <text>Some ID</text>
   </Frame:ID>
   <Frame:Name>
      <text>Some Name </text>
   </Frame:Name>
   <Frame:Name>
      <text>Some Name </text>
   </Frame:Name>
</t>
于 2012-04-20T12:33:15.827 に答える
0

jeevan からの提案に基づいています。

  <xsl:template match="ABC" >
    <ABC>
        <xsl:for-each select ="child::*">
        <xsl:choose>
          <xsl:when test="name()='Name'">
            <xsl:element name="Frame:Name">
              <xsl:apply-templates  select="."/>
           </xsl:element>
          </xsl:when>

          <xsl:when test="name()='ID'">
            <Frame:ID>
              <xsl:apply-templates select="."/>
            </Frame:ID>
          </xsl:when>
            <xsl:when test="name()='Address'">
            <Frame:Address>
              <xsl:apply-templates select="."/>
            </Frame:Address>
          </xsl:when>

          <xsl:when test="name()='Place'">
            <Frame:Place>
              <xsl:apply-templates select="."/>
            </Frame:Place>
          </xsl:when>
     </xsl:choose>
      </xsl:for-each>

于 2012-04-20T11:19:11.687 に答える