而使用XSLT将属性转换为元素时,只会忽略父元素中的属性 [英] while transforming attributes to element with XSLT how ignore only the attributes in parent element

查看:86
本文介绍了而使用XSLT将属性转换为元素时,只会忽略父元素中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XSLT,以将包含所有值的某些xml转换为另一个xml,将值保留为子元素。在我的xml中有两个孩子的名字,一个是aa,另一个是bbb。当我检查aa和bbb的值时,转换工作正常。问题是我想删除/忽略这些孩子元素的父元素中找到的属性。

I am using XSLT in order to transform certain xml that contains all values formated as attributes to another xml keeping the values as child element. In my xml there are two children name, one is aa and another is bbb. The transformation is working perfectly when I check the values for both aa and bbb. The problem is I want to delete/ignore the attributes found in the parent element of these children element.

我下面显示了我迄今为止所尝试过的两个重要的事项。第一个是我原来的想法,但是我想要避免父属性(在我的xml中完全是鳕鱼和包)。

I am showing below the must two significant tentatives I have tried so far. The first one is my original idea but I want it avoiding the parent attributes (exactly "cod" and"package" in my xml).

第二个是后果搜索,但它实际上是远离我想要的。然而,第二个暂时可以帮助我表达,我可能错过了某些模式或一些comand来从根元素跳过属性。

The second is the consequence of searching around but it is actually driving away from what I want. Nevertheless, the second tentative can help me express that I am probably missing either certain "pattern" or some "comand" to skip the attributes from root element.

MyApp.java

MyApp.java

Path f = Paths.get("C:\\in1.xsl");
//Path f = Paths.get("C:\\in2.xsl");
InputStream resourceAsStream = Files.newInputStream(f);
StreamSource xsl = new StreamSource(resourceAsStream);
Transformer transformer;
transformer = TransformerFactory.newInstance().newTransformer(xsl);
StreamSource in = new StreamSource(
              new FileInputStream("C:\\my_xml.xml"));
StreamResult out = new StreamResult(System.out);
transformer.transform(in, out); //in1 produced out1 and in2 produced out2

my_xml.xml

my_xml.xml

<c:product xmlns:c="myapp">
       <c:item cod="789">
              <c:aa name="024" value="123"/>
              <c:bbb name="0105" value="123456"/>
              <c:bbb name="0122" value="T"/>
              <c:aa name="071" value="00000001"/>
       </c:item>
       <c:item package="123" cod="11111">
              <c:aa name="002" value="753"/>
              <c:aa name="003" value="456"/>
              <c:bbb name="0146" value="147852"/>
       </c:item>
</c:product>

in1.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="UTF-8" indent="yes" standalone="no" omit-xml-declaration="yes"/>
       <xsl:template match="@*">
              <xsl:element name="{name(.)}">
                     <xsl:value-of select="."/>
              </xsl:element>
       </xsl:template>
       <xsl:template match="*">
              <xsl:copy>
                     <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
       </xsl:template>
</xsl:stylesheet>

in2.xls(source 复制除了根节点和属性XSLT之外的XML文件内容

in2.xls (source Copy XML file contents except for root node and attribute XSLT)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/>
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="/*">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

out1.xml(我不想在第一个项目中的鳕鱼元素我不想要包和鳕鱼第二项)

out1.xml (I don’t want cod element in first item neither I want package and cod in second item)

<c:product xmlns:c="myapp">
<c:item>
<cod>789</cod>
<c:aa>
<name>024</name>
<value>123</value>
</c:aa>
<c:bbb>

<name>0105</name>

<value>123456</value>

</c:bbb>

<c:bbb>

<name>0122</name>

<value>T</value>

</c:bbb>

<c:aa>

<name>071</name>

<value>00000001</value>

</c:aa>

</c:item>

<c:item>

<package>123</package>

<cod>11111</cod>

<c:aa>

<name>002</name>

<value>753</value>

</c:aa>

<c:aa>

<name>003</name>

<value>456</value>

</c:aa>

<c:bbb>

<name>0146</name>

<value>147852</value>

</c:bbb>

</c:item>

</c:product>

out2.xml(嗯,这里有一个点,我试图利用这个表达我的self:根元素接受不同的处理,如果我发现如何通过忽略它们的属性来处理根元素,并与其他的in.xls混合,我可能会发现我的解决方案)

out2.xml (well, there is a single point here that I tried to take advantage to express my self: the root element receives a different treatment. If I find how to treat the root element by ignoring their attributes and mixed with the rest of in.xls I will probably discover my solution)

      <aa name="024" value="123"/>

      <bbb name="0105" value="123456"/>

      <bbb name="0122" value="T"/>

      <aa name="071" value="00000001"/>

      <aa name="002" value="753"/>

      <aa name="003" value="456"/>

      <bbb name="0146" value="147852"/>

推荐答案

我猜(!)你想做一些类似的事情:

I am guessing (!) you want to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:c="myapp"
exclude-result-prefixes="c">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="c:item">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:element name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

当应用于您的输入示例时,结果将是:

When applied to your input example, the result will be:

<product>
   <item>
      <aa>
         <name>024</name>
         <value>123</value>
      </aa>
      <bbb>
         <name>0105</name>
         <value>123456</value>
      </bbb>
      <bbb>
         <name>0122</name>
         <value>T</value>
      </bbb>
      <aa>
         <name>071</name>
         <value>00000001</value>
      </aa>
   </item>
   <item>
      <aa>
         <name>002</name>
         <value>753</value>
      </aa>
      <aa>
         <name>003</name>
         <value>456</value>
      </aa>
      <bbb>
         <name>0146</name>
         <value>147852</value>
      </bbb>
   </item>
</product>






请注意, c :item 获得特殊处理的元素(第二个模板)是根元素。


Note that the c:item element getting special treatment (2nd template) is not the root element.

这篇关于而使用XSLT将属性转换为元素时,只会忽略父元素中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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