1

考虑我的输入 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>    

转换上述输入 xml 文件的所需 xsl 文件是指 Properties.xml 文件,该文件具有对应于 Properties.xml 下面给出的输入 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.
4

2 回答 2

1

看起来您不需要 Properties 文件,因为您只是摆脱了可以更简单地完成的命名空间:

<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 东西。

编辑

更新,包括能让你更接近目标的东西

<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 的所有属性值移动到元素主体,那么将执行以下操作:

<?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>
于 2012-04-09T11:58:26.387 回答
0

我在 DavidCarlisle 给出的代码的帮助下使用了下面的 xslt 代码,它部分满足了要求

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

我在这方面面临的唯一问题是我无法想出一个 xslt 代码,它可以让具有“值”属性(联系人和动态名称)的节点提取其属性值并将它们放在相应的标签(在 Properties.xml 中指定)内容下. 请浏览这个 xsl 文件并在其中进行必要的更改,或者如果您能想出一个新文件,我会同样高兴。但请确保使用 Properties.xml 文件。谢谢。

于 2012-04-11T05:01:33.153 回答