首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将唯一的id添加到处理指令(xsl)

将唯一的id添加到处理指令(xsl)
EN

Stack Overflow用户
提问于 2016-08-11 14:55:24
回答 2查看 178关注 0票数 0

我想重命名一个处理指令,同时给它添加一个唯一的id属性。我的输入如下:

代码语言:javascript
复制
?xml version="1.0" encoding="utf-8" ?>
<material xml:lang="en-us">
  <title>
    <?PI_start author="joepublic" comment="Comment #1" ?>Discovering
      <?PI_end?>XML
  </title>
  <related-stuff>
    <?PI_start author="johndoe" comment="Comment #3" ?>
      <a href="otherdoc.xml" />
      <?PI_end?>
  </related-stuff>
</material>

结果如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<material xml:lang="en-us">
  <title>
    <?otherPI_start author="joepublic" comment="Comment #1" id ="1" ?>Discovering
    <?otherPI_end id ="1" ?>XML
  </title>
  <related-stuff>
    <?otherPI_start author="johndoe" comment="Comment #3" id ="2" ?>
    <a href="otherdoc.xml" />
    <?otherPI_end id ="2"?>
  </related-stuff>
</material>

注意,我生成了两个id,id="1“表示文档中遇到的第一条指令,id="2”表示第二条。还请注意,id在otherPI_end处理指令中重复。

您能帮我识别xsl中的匹配语句吗?

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="processing-instruction('PI_start')">
    <xsl:processing-instruction name="otherPI_start">
      author="<xsl:value-of select="substring-before(substring-after(.,'author=&quot;'),'&quot;')"/>"
      comment="<xsl:value-of select="substring-before(substring-after(.,'comment=&quot;'),'&quot;')"/>"
      id="<!-- What should I put here??? -->"
    </xsl:processing-instruction>
  </xsl:template>
  <xsl:template match="processing-instruction('PI_end')">
    <xsl:processing-instruction name="otherPI_end">
      id="<!-- What should I put there??? -->"
    </xsl:processing-instruction>
  </xsl:template>
</xsl:stylesheet>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-11 15:44:22

id="<!-- What should I put here??? -->"

代码语言:javascript
复制
<xsl:value-of select="generate-id()"/>

如果希望<?PI_end?>的id与前面的<?PI_start ?>匹配,请使用:

代码语言:javascript
复制
<xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/>

XSLT1.0

代码语言:javascript
复制
<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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="processing-instruction('PI_start')">
    <xsl:processing-instruction name="otherPI_start">
        <xsl:value-of select="."/>
        <xsl:text>id="</xsl:text>
        <xsl:value-of select="generate-id()"/>
        <xsl:text>"</xsl:text>
    </xsl:processing-instruction>
</xsl:template>

<xsl:template match="processing-instruction('PI_end')">
    <xsl:processing-instruction name="otherPI_end">
        <xsl:text>id="</xsl:text>
        <xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/>
        <xsl:text>"</xsl:text>
    </xsl:processing-instruction>
</xsl:template>    

</xsl:stylesheet>
票数 1
EN

Stack Overflow用户

发布于 2016-08-11 15:43:05

您可以在这里使用xsl:number

代码语言:javascript
复制
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
  author="<xsl:value-of select="substring-before(substring-after(.,'author=&quot;'),'&quot;')"/>"
  comment="<xsl:value-of select="substring-before(substring-after(.,'comment=&quot;'),'&quot;')"/>"
    id="<xsl:number count="processing-instruction('PI_start')" level="any" />"
</xsl:processing-instruction>
</xsl:template>

如果您发现这不包括当前的处理指令(这意味着从零开始编号),请尝试以下操作.

代码语言:javascript
复制
<xsl:number count="/|processing-instruction('PI_start')" level="any" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38899819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档