English 中文(简体)
Mangling IDs and References to IDs in XML
原标题:
  • 时间:2009-11-12 22:35:11
  •  标签:
  • xml
  • xslt
  • svg

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the references to them. (I m doing this with SVGs to add a little context)

Say I have:

<bar id="foo"/>
<baz ref="url(#foo)"/>
<bar id="abc"/>
<baz ref="asdf:url(#abc)"/>

I d like a way to automatically turn that into something like:

<bar id="foo_1"/>
<baz ref="url(#foo_1)"/>
<bar id="abc_1"/>
<baz ref="asdf:url(#abc_1)"/>

or something similar.

I can probably write some XSL to do it, but was hoping there s an easier way.

Thanks!

最佳回答

If you end up using XSLT, you may find the generate-id function useful for generating ids.

Here s a sort of dummy example using XSLT 1.0:

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

  <xsl:key name="element-by-id" match="//*" use="@id"/>

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

  <!-- ... except for rewritten id s -->
  <xsl:template match="@id">
    <xsl:attribute name="id">
      <xsl:value-of select="generate-id(..)"/>
    </xsl:attribute>
  </xsl:template>

  <!-- ... and rewritten id references -->
  <xsl:template match="@ref">
    <xsl:variable name="head" select="substring-before(.,  url(# )"/>
    <xsl:variable name="tail" select="substring-after(.,  url(# )"/>
    <xsl:variable name="idref" select="substring-before($tail,  ) )"/>
    <xsl:variable name="end" select="substring-after($tail,  ) )"/>
    <xsl:attribute name="ref">
      <xsl:value-of select="concat($head,  url(# , 
                            generate-id(key( element-by-id , $idref)), 
                             ) , $end)"/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

If you don t like the id s produced by generate-id (or if you cannot use it for other reasons -- to ensure that you get unique id s all the nodes need to be processed within the same transformation) you can replace the calls to it with some other logic, like adding a suffix.

问题回答

Not a very elegant solution, but you could always use some regular expressions.

Match on id=(.*) and then replace all #$1 s with whatever you want.





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签