小编ati*_*tif的帖子

向节点添加属性

如果子节点值等于某个字符串,我试图向该节点添加一个属性.

我有一个main.xml文件

<Employees>    
 <Employee>     
 <countryid>32</countryid>
 <id name="id">1</id>
 <firstname >ABC</firstname>
 <lastname >XYZ</lastname>     
 </Employee>
 <Employee>     
 <countryid>100</countryid>
 <id name="id">2</id>
 <firstname >ddd</firstname>
 <lastname >ggg</lastname>     
 </Employee>
 </Employees>    
Run Code Online (Sandbox Code Playgroud)

因此,假设国家标识等于32,则应将属性country = 32添加到Employee节点.输出应如下所示:

与Output.xml

 <Employees>    
 <Employee countryid="32">     
 <countryid>32</countryid>
 <id name="id">1</id>
 <firstname >ABC</firstname>
 <lastname >XYZ</lastname>     
 </Employee>
 <Employee>     
 <countryid>100</countryid>
 <id name="id">2</id>
 <firstname >ddd</firstname>
 <lastname >ggg</lastname>     
 </Employee>
 </Employees>    
Run Code Online (Sandbox Code Playgroud)

我使用以下脚本,但得到的错误是在包含元素的子元素之后无法创建属性节点:

Transform.xsl


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

<xsl:template match="Employees/Employee/countryid[.=32']">
  <xsl:attribute name="countryid">32</xsl:attribute>
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.我们也可以将countryid作为逗号分隔值传递,这样我就可以传递32,100,然后它应该为所有匹配的节点添加属性.

谢谢.

xslt xslt-2.0

9
推荐指数
2
解决办法
4万
查看次数

xslt调用模板与动态匹配

我试图调用动态参数,同时调用模板来抑制xml中的节点.

我会称这个模板为:

transform employee.xml suppress.xsl ElementsToSuppress=id,fname 
Run Code Online (Sandbox Code Playgroud)

employee.xml

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee>
    <id>1</id>
    <firstname>xyz</firstname>
    <lastname>abc</lastname>
    <age>32</age>
    <department>xyz</department>
  </Employee>
  <Employee>
    <id>2</id>
    <firstname>XY</firstname>
    <lastname>Z</lastname>
    <age>21</age>
    <department>xyz</department>
  </Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)

Suppress.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0" xmlns:elements="http://localhost">

  <elements:name abbrev="id">id</elements:name>
  <elements:name abbrev="fname">firstname</elements:name>

  <xsl:param name="ElementsToSuppress" ></xsl:param>

  <xsl:variable name="tokenizedSample" select="tokenize($ElementsToSuppress,',')"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <xsl:for-each select="$tokenizedSample">
      <xsl:call-template name ="Suppress"  >
        <xsl:with-param  name="parElementName">
          <xsl:value-of select="."/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>






  <xsl:template name="Suppress">
    <xsl:param name="parElementName" select="''"></xsl:param>
    <xsl:variable name="extNode" select="document('')/*/elements:name[@abbrev=$parElementName]"/>
    <xsl:call-template name="test" >
      <xsl:with-param name="parElementName" >
        <xsl:value-of …
Run Code Online (Sandbox Code Playgroud)

xslt xslt-2.0 xslt-1.0

6
推荐指数
2
解决办法
6829
查看次数

sgml 到 xml 的转换

我的 .sgm 文件中有以下示例 sgml 数据,我想将其转换为 xml

<?dtd name="viewed">
<?XMLDOC>
<viewed >xyz
<cite>
<yr>2010
<pno cite="2010 abc 1188">10
<?/XMLDOC>

<?XMLDOC>
<viewed>abc.
<cite>
<yr>2010
<pno cite="2010 xyz 5133">9
<?/XMLDOC>
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的:

<index1>
    <num viewed="xyz"/>
    <heading>xyz</heading>
    <index-refs>
      <link  caseno="2010 abc 1188</link>
    </index-refs>
  </index-1>
<index1>
    <num viewed="abc"/>
    <heading>abc</heading>
    <index-refs>
      <link  caseno="2010 xyz 5133</link>
    </index-refs>
  </index-1>
Run Code Online (Sandbox Code Playgroud)

这可以在 c# 中完成还是我们可以使用 xslt 2.0 来进行这种转换?

xslt sgml c#-2.0

5
推荐指数
2
解决办法
9055
查看次数

xslt中的第一个子节点名称

我想知道如何在xslt中找到特定节点的第一个子节点名称.

我有一个xml:

 <name>
    <body>
      <para>
        <text> some text</text>
      </para>
    </body>
  </name>
Run Code Online (Sandbox Code Playgroud)

我可以使用body/node()[1]/local-name()获取名称吗?

<xsl:template match="name">
<name> 
<xsl:variable name="firstchild" select="body/node()[1]/local-name()">
                        </xsl:variable>
 <xsl:value-of select="$firstchild" />
 </name>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

输出应该是

 <name>
    para
  </name> 
Run Code Online (Sandbox Code Playgroud)

xslt

5
推荐指数
1
解决办法
1万
查看次数

使用XSL缩进XML

什么XSL脚本会缩进我的数据?

例如:

 <dtd name="cited">
 <XMLDOC>
 <cited year="2010">
 <case>
 No.&nbsp;275 v. M.N.R. 
 <cite>
 <yr>
 2010 
 <pno cite="20101188">10</pno> 
 </yr>
 </cite>
 </case>
 </cited>
 </XMLDOC>
 <XMLDOC>
 <case>
 Wellesley St.
 <cite>
 <yr>
 2010 
 <pno cite="20105133">9</pno> 
 </yr>
 </cite>
 </case>
 </XMLDOC>
 </dtd>
Run Code Online (Sandbox Code Playgroud)

至:

<dtd name="cited">
  <XMLDOC>
    <cited year="2010"></cited>
    <case>
      No.&nbsp;275 v. M.N.R.
    </case> 
    <cite>
    </cite>
    <yr>
      2010 
    </yr>
    <pno cite="20101188">10</pno> 
  </XMLDOC>
  <XMLDOC>
    <case>
      Wellesley St 
    </case>
    <cite>
    </cite>
    <yr>
      2010 
    </yr>
    <pno cite="20105133">9</pno> 
  </XMLDOC>
</dtd>
Run Code Online (Sandbox Code Playgroud)

谢谢!

有关

sgml到xml转换

来自评论:

我想要的是应用正确的结束标签,如

<yr></yr>
<pno cite="20101188">10</pno>
Run Code Online (Sandbox Code Playgroud)

代替

<yr>
2010 …
Run Code Online (Sandbox Code Playgroud)

xslt

4
推荐指数
2
解决办法
2万
查看次数

xslt中的动态xpath?

我有以下文件集:

SourceFile.xml:

      <?xml version="1.0" encoding="utf-8" ?>
     <Employees>
     <Employee id="1">
          <firstname relationship="headnote">Atif</firstname>
          <lastname relationship="lname">Bashir</lastname>
          <age relationship="age">32</age>
          </Employee>
     </Employees>
Run Code Online (Sandbox Code Playgroud)

ParamerterSettings.xml

        <?xml version="1.0" encoding="utf-8"?>
        <Settings>
        <Employee id="1">
             <sourceFile>Lookup1.xml</sourceFile>
             <sourceXpathfield>Employees/Employee[@id</sourceXpathfield>
             <lookupXpathfield>Employees/Employee[@id='1']</lookupXpathfield>
             <elementstoinsert>xyz</elementstoinsert>
             </Employee>
         </Settings>
Run Code Online (Sandbox Code Playgroud)

Lookup.xml

<?xml version="1.0" encoding="utf-8"?>
 <Employees>
  <Employee id="1">
      <department code="102">HR</department>
   </Employee>
   </Employees>
Run Code Online (Sandbox Code Playgroud)

transform.xsl

  <?xml version="1.0" encoding="UTF-8" ?>
   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

   <xsl:include href="identity.xsl"/>

  <xsl:param name="EmployeeId" select="'1,2'" />
  <xsl:variable name="FileSettings" select="document('test3.xml')" />
  <xsl:variable name="SuppressSetting" select="$FileSettings/Settings/Employee[@id = tokenize($EmployeeId, ',')]" />

  <xsl:template match="Employee">
  <xsl:copy>
  <xsl:apply-templates select="@*"/>
  <xsl:apply-templates select="publisher" />
  <xsl:apply-templates select="node() except publisher"/> …
Run Code Online (Sandbox Code Playgroud)

xslt xslt-2.0

4
推荐指数
1
解决办法
1万
查看次数

xslt xsl:分析字符串和正则表达式

我有一个 xml 文件,我必须在其中查找模式并在匹配的特定模式周围放置一个“标记。

<xml>
<foo>
    <id>1</id>
    <description>This is section 1, this is section 1 or 2, this is section 1 and 2</description>
</foo>
</xml>
Run Code Online (Sandbox Code Playgroud)

现在在文件中我必须找到一个模式“section 1”,“section 1 or 2”和“section 1 and 2”,并在匹配的单词周围放置一个“标记。

我写了一个xslt,如下所示:

  <xsl:template match="text()">

<xsl:analyze-string select="." regex="section\s\d+|section\s\d+\s+or\s+\d|section\s\d+\s+and\s+\d">
  <xsl:matching-substring>
    <xsl:if test="matches(.,'section\s\d+')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
      <xsl:text>"</xsl:text>       
    </xsl:if>
    <xsl:if test="matches(.,'section\s\d+\s+or\s+\d')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
        <xsl:text>"</xsl:text>       
    </xsl:if>
    <xsl:if test="matches(.,'section\s\d+\s+and\s+\d')" >
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>     
      <xsl:text>"</xsl:text>       
    </xsl:if>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <xsl:value-of select="current()"/>
  </xsl:non-matching-substring>
</xsl:analyze-string>
 </xsl:template>
Run Code Online (Sandbox Code Playgroud)

我在这里面临的问题是模式“第 1 节”也匹配所有其他模式,所以我没有得到想要的结果

上述变换结果为

<xml>
  <foo>
      <id>1</id>
      <description>This is "section …
Run Code Online (Sandbox Code Playgroud)

xslt

3
推荐指数
1
解决办法
2万
查看次数

匹配第一次出现的子节点

我有一个xml文档

 <body>
      <heading>Main Heading</heading>
      <para>
       <text>para 1</text>
      </para>
      <para>
        <heading>heading 2</heading>
        <text> para 2</text>
      </para>
      <para>
         <heading>heading 3</heading>
         <text>para 3</text>
       </para>
    </body>
Run Code Online (Sandbox Code Playgroud)

我想匹配第一个出现的标题元素并添加一些文本.我只想要第一次出现的标题和标题应该是para元素的子节点.

所以输出应该如下

 <body>
      <heading>Main Heading</heading>
      <para>
       <text>para 1</text>
      </para>
     <para>
        <heading>**This is a first heading found** heading 2</heading>
        <text> para 2</text>
      </para>
     <para>
    <heading>heading 3</heading>
        <text>para 3</text>
      </para>
    </body>
Run Code Online (Sandbox Code Playgroud)

任何提示如何开始是值得赞赏的.

xslt xslt-2.0

2
推荐指数
1
解决办法
6551
查看次数

标签 统计

xslt ×8

xslt-2.0 ×4

c#-2.0 ×1

sgml ×1

xslt-1.0 ×1