English 中文(简体)
需要将Xml文档改成另一个xml,使用Xslt,其xsl文档使用一种特性。
原标题:need to transform an xml file into another xml using xslt whose xsl file uses a Properties.xml file

考虑我的投入xml文档如下:

     <?xml version="1.0" encoding="UTF-8"?>
        <com xsi:schemaLocation="http://do.way.com/sales/Amb
           http://do.way.com/temp/sales/ale/ax.xsd" 
           xmlns:w="http://do.way.com/sales/W" 
           xmlns="http://do.way.com/sales/Amb" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:wire="http://do.way.com/sales/Wire">

    <content>
      <wire:wire>
        <wire:sI>

          <w:aH>
          <text>Ccc  <dynamic name="C_R_N"/>: More</text>
          <w:contact value="false"/>
          </w:aH>

          <w:page>
            <nL>Please call us</nL>
          </w:page>

          <w:body>
            <text> C R N: <strong>
           <dynamic name="C_R_N"/></strong>
            </text>
          </w:body>

          <w:body>
            <text>
              RE: <dynamic name="D_C_P"/> <dynamic name="M_C_O"/>
            </text>
          </w:body>

          <w:body>
            <text>
              In order <strong>
                <dynamic name="M_D_D"/>
              </strong>, we need some information.
            </text>
          </w:body>

          <w:body>
            <text>
              <strong>
                Please call us <dynamic name="C_P_D_N"/>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic name="DST_M_F_S_Hour"/> am - 
    <dynamic name="DST_M_F_E_Hour"/> pm; Saturday, 
    <dynamic name="DST_M_F_S_Hour"/> am - 
    <dynamic name="DST_M_F_E_Hour"/> pm.
                </li>
                       </ul>
            </text>
          </w:body>

          <w:body>
            <text>
              Thank you for your  action. .
            </text>
          </w:body>


          <w:body>
            <text>
              <dynamic name="C_D_N"/>
            </text>
          </w:body>


          <w:TextAndImage>
            <text>
             <dynamic name="C_S_N_D"/>            
            </text>
          </w:TextAndImage>

          <w:dynamicNames>
            <w:dynamicName value="LOW"/>
            <w:dynamicName value="Env"/>
          </w:dynamicNames>

          </wire:sI>
        </wire:wire>

          </content>
          </com>    

the required xsl file which transforms the above input xml file refers to a Properties.xml file which has the respective transformed tags(values) for the input xml tag elements(keys) given below Properties.xml

      <?xml version="1.0" encoding="utf-8"?>

            <Properties>     
            <Property value="w:aH">aHe</Property>
           <Property value="w:contact">contact1</Property>
           <Property value="w:page">pageH</Property> 
           <Property value="w:body">bodyP</Property>
           <Property value="w:TextAndImage">textAndImage1</Property>
           <Property value="w:dynamicNames">dynamicNames1</Property>
           <Property value="w:dynamicName">dynamicValName</Property>
         </Properties> 

和我转变的xml格

    <Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd" 
 xmlns="id:d1234" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


     <aHe>
      <text>Ccc  
             <dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml" >
                  C_R_N
            </dynamic>
        : More
    </text>
      <contact1>false</contact1>
      </aHe>

      <pageH>
        <nL>Please call us</nL>
      </pageH>

      <bodyP>
        <text> C R N: <strong>
       <dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml">
      C_R_N
     </dynamic>
      </strong>
        </text>
      </bodyP>

      <bodyP>
        <text>
          RE: <dynamic name="D_C_P" xmlns="http://www.w3.org/1999/xhtml"> 
              D_C_P</dynamic>
             <dynamic name="M_C_O" xmlns="http://www.w3.org/1999/xhtml">M_C_O</dynamic>
        </text>
      </bodyP>

      <bodyP>
        <text>
          In order <strong>
            <dynamic name="M_D_D" xmlns="http://www.w3.org/1999/xhtml"> M_D_D </dynamic>
          </strong>, we need some information.
        </text>
      </bodyP>

      <bodyP>
        <text>  Please call us 
          <strong>
           <dynamic name="C_P_D_N" xmlns="http://www.w3.org/1999/xhtml">C_P_D_N</dynamic>.
          </strong> Our hours <ul class="nested">
            <li>
               Monday - Friday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_E_Hour</dynamic> pm; Saturday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">
              DST_M_F_E_Hour</dynamic>pm.
            </li>
                   </ul>
        </text>
      </bodyP>

      <bodyP>
        <text>
          Thank you for your  action. .
        </text>
      </bodyP>


      <bodyP>
        <text>
          <dynamic name="C_D_N" xmlns="http://www.w3.org/1999/xhtml">
          C_D_N </dynamic>
        </text>
      </bodyP>


      <textAndImage>
        <text>
          <dynamic name="C_S_N_D" xmlns="http://www.w3.org/1999/xhtml">
           C_S_N_D </dynamic>
        </text>
      </textAndImage>

      <dynamicNames1>
        <dynamicValName>LOW</dynamicValName>
        <dynamicValName>Env</dynamicValName>
      </dynamicNames1>

     </Content>


      Can anyone  provide me with the xsl file which meets my requirement.
问题回答

它认为,你不需要财产档案,因为你只是拿走可以做更简单得多的名词:

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

你想要实现的其余部分可能是平原的斜体。

http://www.ohchr.org。

最新信息,包括使你更接近您目标的障碍

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

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

<xsl:template match="* [local-name() !=  com  and local-name() !=  content  and local-name() !=  wire  and local-name() !=  sI ]">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node() | @*"/>
    </xsl:element>
</xsl:template>

<xsl:template match="* [local-name() =  dynamic ]">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node() | @*"/>
        <xsl:value-of select="@name"/>
    </xsl:element>
</xsl:template>

<xsl:template match="* [local-name() =  contact  or local-name()= dynamicName ]">
    <xsl:element name="{local-name()}">
        <xsl:value-of select="@value"/>
    </xsl:element>
</xsl:template>

结果是

<?xml version="1.0" encoding="UTF-8"?>
<Content>
    <aH>
        <text>Ccc  <dynamic name="C_R_N">C_R_N</dynamic>: More</text>
        <contact>false</contact>
    </aH>
    <page>
        <nL>Please call us</nL>
    </page>
    <body>
        <text> C R N: <strong>
                <dynamic name="C_R_N">C_R_N</dynamic>
            </strong></text>
    </body>
    <body>
        <text>
              RE: <dynamic name="D_C_P">D_C_P</dynamic><dynamic name="M_C_O">M_C_O</dynamic></text>
    </body>
    <body>
        <text>
              In order <strong>
                <dynamic name="M_D_D">M_D_D</dynamic>
            </strong>, we need some information.
            </text>
    </body>
    <body>
        <text><strong>
                Please call us <dynamic name="C_P_D_N">C_P_D_N</dynamic>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am - 
    <dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm; Saturday, 
    <dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am - 
    <dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm.
                </li>
            </ul></text>
    </body>
    <body>
        <text>
              Thank you for your  action. .
            </text>
    </body>
    <body>
        <text>
            <dynamic name="C_D_N">C_D_N</dynamic>
        </text>
    </body>
    <TextAndImage>
        <text>
            <dynamic name="C_S_N_D">C_S_N_D</dynamic>
        </text>
    </TextAndImage>
    <dynamicNames>
        <dynamicName>LOW</dynamicName>
        <dynamicName>Env</dynamicName>
    </dynamicNames>
</Content>

如果你希望把所有属性动态/“姓名和其他要素/价值”的属性价值移至要素机构,那么:

<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/">
        <Content>
            <xsl:apply-templates/>
        </Content>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:copy/>
    </xsl:template>
    <xsl:template match="* [local-name() !=  com  and local-name() !=  content  and local-name() !=  wire  and local-name() !=  sI ]">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="* [local-name() =  dynamic ]">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*[local-name != name ]"/>
            <xsl:value-of select="@name"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="* [local-name() !=  dynamic  and @value]">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*[local-name != value ]"/>
            <xsl:value-of select="@value"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<Content>
    <aH>
        <text>Ccc  <dynamic>C_R_N</dynamic>: More</text>
        <contact>false</contact>
    </aH>
    <page>
        <nL>Please call us</nL>
    </page>
    <body>
        <text> C R N: <strong>
                <dynamic>C_R_N</dynamic>
            </strong></text>
    </body>
    <body>
        <text>
              RE: <dynamic>D_C_P</dynamic><dynamic>M_C_O</dynamic></text>
    </body>
    <body>
        <text>
              In order <strong>
                <dynamic>M_D_D</dynamic>
            </strong>, we need some information.
            </text>
    </body>
    <body>
        <text><strong>
                Please call us <dynamic>C_P_D_N</dynamic>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic>DST_M_F_S_Hour</dynamic> am - 
    <dynamic>DST_M_F_E_Hour</dynamic> pm; Saturday, 
    <dynamic>DST_M_F_S_Hour</dynamic> am - 
    <dynamic>DST_M_F_E_Hour</dynamic> pm.
                </li>
            </ul></text>
    </body>
    <body>
        <text>
              Thank you for your  action. .
            </text>
    </body>
    <body>
        <text>
            <dynamic>C_D_N</dynamic>
        </text>
    </body>
    <TextAndImage>
        <text>
            <dynamic>C_S_N_D</dynamic>
        </text>
    </TextAndImage>
    <dynamicNames>
        <dynamicName>LOW</dynamicName>
        <dynamicName>Env</dynamicName>
    </dynamicNames>
</Content>

我使用了以下xslt代码,帮助了David Carlisle提出的部分满足要求的守则。

     <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
       xmlns="id:d1234"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
       xmlns:w="http://do.way.com/sales/W" 
       xmlns:wire="http://do.way.com/sales/Wire"    
       exclude-result-prefixes=" wire w ">

         <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
         <xsl:template match="/">
           <Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd">
            <xsl:apply-templates select="w:sI"/>
          </Content>
        </xsl:template>

         <xsl:template match="*[local-name()!=sI]">
            <xsl:variable name="n" select="name()"/>
              <xsl:variable name="new">
                <xsl:for-each select="document( Properties.xml )">
                 <xsl:choose>
                  <xsl:when test="key( n ,$n)">
                    <xsl:value-of select="key( n ,$n)"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$n"/>
                  </xsl:otherwise>
                </xsl:choose>
               </xsl:for-each>
              </xsl:variable>

       <xsl:element name="{$new}">
          <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
          </xsl:element>
       </xsl:template>


     <xsl:key name="n" match="Property" use="@value"/>

        <xsl:template match="*[name() =  dynamic ]">
            <dynamic xmlns="http://www.w3.org/1999/xhtml">
               <xsl:copy-of select="@*"/>
                   <strong>
              <xsl:value-of select="@name"/>
             </strong>
           </dynamic>

        </xsl:template>
    </xsl:stylesheet>

the only problem im facing in this is i can t comeup with an xslt code which can take the nodes having value attributes (contact and dynamicName) extract its attribute value and put them under respective tag s (specified in Properties.xml) content. Please go through this xsl file and make necessary changes in it or I would be equally glad if you can come up with a new one.But please make sure of using Properties.xml file.Thanks.





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签