需要使用xslt将其xml文件转换为另一个xml,其xsl文件使用Properties.xml文件 [英] need to transform an xml file into another xml using xslt whose xsl file uses a Properties.xml file

查看:91
本文介绍了需要使用xslt将其xml文件转换为另一个xml,其xsl文件使用Properties.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下所示考虑我的输入xml文件

Consider my input xml file as below

     <?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>    

转换上面的输入xml文件所需的xsl文件是指一个Properties.xml文件,该文件具有下面给出的输入xml标记元素(键)的相应转换后的标记(值) Properties.xml

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看起来像

and my transformed xml looks like

    <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.

推荐答案

您似乎不需要Properties文件,因为您只需要删除可以更简单地完成的命名空间即可:

It looks like you don't need the Properties file as you are only getting rid of namespaces which can be done much simpler:

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

您想要实现的其余目标可能是普通的xslt东西.

The rest of what you want to achieve is probably plain xslt stuff.

编辑

更新内容,包括可以使您更接近目标的东西

Update including stuff that will get you much closer to your goal

<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>

相反,如果您希望将dynamic/@ name属性和otherElement/@ value属性的所有属性值移动到元素主体,则将执行以下操作:

If, instead, you wish to move all attribute values for attributes dynamic/@name and otherElement/@value to the element body, then the following will do:

<?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将其xml文件转换为另一个xml,其xsl文件使用Properties.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆